<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/55515>55515</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Debug location information dropped unnecessarily in div-rem-pairs optimization pass.
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          cbossut7
      </td>
    </tr>
</table>

<pre>
    Overview: In the attached example, the div-rem-pairs pass will drop debug location unnecessarily.

Steps to reproduce (files attached at the end):

Here is the version of llvm:
$ ../llvm-build/bin/clang++ --version
clang version 15.0.0 (https://github.com/llvm/llvm-project.git f422c5d87142cc1dc65cf7fb15444c2afc810611)
Target: x86_64-unknown-linux-gnu
Thread model: posix

$ opt -div-rem-pairs debugified_out.ll > debugified_opt.bc
$ llvm-dis debugified_opt.bc

$ diff debugified_out.ll debugified_opt.ll 
1c1
< ; ModuleID = 'bugpoint-reduced-simplified.ll'
---
> ; ModuleID = 'debugified_out.ll'
3c3
< target triple = "x86_64-unknown-linux-gnu"
---
> target triple = "x86_64-unknown-unknown"
11,12c11,12
<   %rem = sub i16 %a, %t0, !dbg !15
<   call void @llvm.dbg.value(metadata i16 %rem, metadata !12, metadata !DIExpression()), !dbg !15
---
>   %rem.recomposed = srem i16 %a, %b
>   call void @llvm.dbg.value(metadata i16 %rem.recomposed, metadata !12, metadata !DIExpression()), !dbg !15

The variable %rem seems to have a one-to-one relationship with %rem.recomposed, so it seems the debug location !15 is unnecessarily dropped, according to the principles in:
    https://llvm.org/docs/HowToUpdateDebugInfo.html

There's a one-to-one relationship between the new and old instruction, and it's placed in the same location, so the DebugLoc should be preserved in the reproducer.

Relevant files:
File 1—------------------------------------------------------------------------------
$ cat debugified_out.ll
; ModuleID = 'bugpoint-reduced-simplified.ll'
source_filename = "div-expanded-rem-pair.ll"
target triple = "x86_64-unknown-linux-gnu"

define void @hoist_srem(i16 %a, i16 %b) !dbg !5 {
entry:
  %div = sdiv i16 %a, %b, !dbg !13
  call void @llvm.dbg.value(metadata i16 %div, metadata !9, metadata !DIExpression()), !dbg !13
  %t0 = mul i16 %div, %b, !dbg !14
  call void @llvm.dbg.value(metadata i16 %t0, metadata !11, metadata !DIExpression()), !dbg !14
  %rem = sub i16 %a, %t0, !dbg !15
  call void @llvm.dbg.value(metadata i16 %rem, metadata !12, metadata !DIExpression()), !dbg !15
  unreachable, !dbg !16
}

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

attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

!llvm.dbg.cu = !{!0}
!llvm.debugify = !{!2, !3}
!llvm.module.flags = !{!4}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "bugpoint-reduced-simplified.ll", directory: "/")
!2 = !{i32 4}
!3 = !{i32 3}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = distinct !DISubprogram(name: "hoist_srem", linkageName: "hoist_srem", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!6 = !DISubroutineType(types: !7)
!7 = !{}
!8 = !{!9, !11, !12}
!9 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
!11 = !DILocalVariable(name: "2", scope: !5, file: !1, line: 2, type: !10)
!12 = !DILocalVariable(name: "3", scope: !5, file: !1, line: 3, type: !10)
!13 = !DILocation(line: 1, column: 1, scope: !5)
!14 = !DILocation(line: 2, column: 1, scope: !5)
!15 = !DILocation(line: 3, column: 1, scope: !5)
!16 = !DILocation(line: 4, column: 1, scope: !5)

File 2-----------------------------------------------------------------------
$ cat debugified_opt.ll
; ModuleID = 'debugified_out.ll'
source_filename = "div-expanded-rem-pair.ll"
target triple = "x86_64-unknown-unknown"

define void @hoist_srem(i16 %a, i16 %b) !dbg !5 {
entry:
  %div = sdiv i16 %a, %b, !dbg !13
  call void @llvm.dbg.value(metadata i16 %div, metadata !9, metadata !DIExpression()), !dbg !13
  %t0 = mul i16 %div, %b, !dbg !14
  call void @llvm.dbg.value(metadata i16 %t0, metadata !11, metadata !DIExpression()), !dbg !14
  %rem.recomposed = srem i16 %a, %b
  call void @llvm.dbg.value(metadata i16 %rem.recomposed, metadata !12, metadata !DIExpression()), !dbg !15
  unreachable, !dbg !16
}

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

attributes #0 = { nofree nosync nounwind readnone speculatable willreturn }

!llvm.dbg.cu = !{!0}
!llvm.debugify = !{!2, !3}
!llvm.module.flags = !{!4}

!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "bugpoint-reduced-simplified.ll", directory: "/")
!2 = !{i32 4}
!3 = !{i32 3}
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = distinct !DISubprogram(name: "hoist_srem", linkageName: "hoist_srem", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !8)
!6 = !DISubroutineType(types: !7)
!7 = !{}
!8 = !{!9, !11, !12}
!9 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !10)
!10 = !DIBasicType(name: "ty16", size: 16, encoding: DW_ATE_unsigned)
!11 = !DILocalVariable(name: "2", scope: !5, file: !1, line: 2, type: !10)
!12 = !DILocalVariable(name: "3", scope: !5, file: !1, line: 3, type: !10)
!13 = !DILocation(line: 1, column: 1, scope: !5)
!14 = !DILocation(line: 2, column: 1, scope: !5)
!15 = !DILocation(line: 3, column: 1, scope: !5)
!16 = !DILocation(line: 4, column: 1, scope: !5)



</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWVtv2zYU_jX2C2FBoiRbefBDEsdrsK4b1rR7DCiKtrnKpEBSTrJfv3MoyZacNI2DFhu2BI4u1LlfPh5buS4e5r_uhNlJcTeKz8m1Im4jCHOO8Y0oiLhn26oUI3rp1wu5mxixnVRMGksqZi25k2VJCqMrUoi8XpNSc-akVqRWSnBhLTOyfAhG4WIUnjfHj05UljhNjKiMLmouyIhmK1kKe9DMnNcoVDGiZ2BaX8A7YQSR1hOA8RbV6RUpy932QEkTEgQjusTVSV7LEgQtc6ngyEum1iN6AR8ymbQSGjb_aC80SoMwCNG6jXOVReF0CZ-1dJs6D7jetgo6PeDPn4K7AAjIKqGUp0U2ixLKeVTwacpXs1UepUmScMpWPIvCaRShf173DTNr4TAN99n0dppMavVF6Ts1KaWq7ydrVbd0GyNYQba6ECVSV9rK-3580HddOTIZ5ssnSK6kKG517QLI2yi-GqxWLsj5QYZ3qZD2qzR7ykKuVk_IP-JDjZ4l4lHLG1-CERfkFyiDUlwv4Ab-6QzYKi2VA-uxPoqJlVCHXhJIAYKGfTKZdHKunpTzyKQ9a8zjgwnOR544I6HYW1761SRQ-oT2l4hoz3sBmPvLiPL2fLCHAHMKifNybJ0TGU1xiWEjwtmFzUVU5Gs8RWmflzMI9E7LgoySEHMYAFmwY2UNjZxthWMFc6yTCWpQ2H4ZxdHjlcX11X1loJuxUWiGJYufp2wYRKVzJDACmgUKFTrb-4TOHTmV99lO9qGn4bu607UcIA0gGcsxuU1urBBbD2MbtgPIJFqJidMTOAGwlR4E7UZWgJBu87SRVhPAiVYOwusQQb0ViHMDKPVgWzUCGOfaFBIQC8xACZWRimMFWgJI12EhxJMMAcxHVBsAwWWhuYXTO313oz9VECGxQDOu1UoHG7ctj8JgIP4z-4y7uXB3QjT7iBJ3hKmC6LIAe6wzNXc-5Jd-WTovqyoZtDgQeB7LtmIfgzZKuO6teq85sR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