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

    <tr>
        <th>Summary</th>
        <td>
            [DebugInfo] IndVars applies wrong debug location after hoisting
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            wrong-debug,
            debuginfo
      </td>
    </tr>

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

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

<pre>
    Reduced from the code in #70951.

https://godbolt.org/z/P4GGcPsbe

```
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define fastcc i32 @s(i8 %a, i1 %c, ptr %p) {
entry:
  br label %for.cond, !dbg !4

for.cond:
 %inc = add i8 %a, 1, !dbg !7
  br i1 %c, label %if.then, label %return

if.then:
  br i1 %c, label %if.then2, label %return

if.then2:
  br label %for.cond

return:
  store i8 %inc, ptr %p, align 1
  ret i32 0
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3}

!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, globals: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "test.c", directory: "/")
!2 = !{}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = !DILocation(line: 8, column: 3, scope: !5)
!5 = distinct !DISubprogram(name: "s", scope: !1, file: !1, line: 6, type: !6, scopeLine: 6, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagLocalToUnit | DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !2)
!6 = !DISubroutineType(types: !2)
!7 = !DILocation(line: 18, column: 5, scope: !8)
!8 = distinct !DILexicalBlock(scope: !9, file: !1, line: 8, column: 12)
!9 = distinct !DILexicalBlock(scope: !10, file: !1, line: 8, column: 3)
!10 = distinct !DILexicalBlock(scope: !5, file: !1, line: 8, column: 3)
```

Run `opt -S --passes=indvars` (or see the godbolt link above), observe the `add i8 %a, 1` gets hoisted into `entry` and its source attribution transforms from `line: 18` to `line: 8`. It's inheriting the `DILocation` from `entry`s terminator, which is not ideal. Should probably be line 0 for this case?


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJycVsuO2zoS_Rp6U7BBUQ_LCy8cG84Y00iCdCazDCixJHNCkQJJddL5-gEp2ZY7jb65FzBoslTPU4cP7pxsNeKW5O9IfljwwZ-N3X7c_-uZd25RGfG8_YxiqFFAY00H_oxQG4EgNRCWrukmT1aEHgjdjePZ-96RdEfYkbBja0RllF8Z2xJ2_EXY8VP2_n39yVU4NyIFnX5x6blt0YPgniv-bAYPJD0AYQyXHUl3uOzZmpJ0l7I4hGVyvwyzIovDUl4nCStJuktYuWxKOs10FBUX8yJbPgY1xu5S8Vb2Ci9p_CyLb0W2HPR3bX7opZJ6-Lls9XC1GkeBjdQIDXe-rkGmDEhGHWGlLIGwnBO2B5mEaR2mvbdh3hO2AbJ-N_pA7e1zgDOuACoLileogmZj7Ko2WgRjwhJRteEvm6dwVbl6ICyXuo6VcCFglkpy72c9CznL8hpdNit_Rn0ns-gHq-cJXLTuKnjDHfszf-yvIJmZTE5uBs4bi1PlUr_Afg9cyVZDctG26GPvJm6S9eGOuSxR6qlbiapd1cNEkCS0jyX0pjtpdUYMCleN4q27101_90ujhpDOS137oHo47U3XS4X_0dITViqu24G3SNIdHP777WH34f23fRL72EgVxYSN696asIvtKGN1MA10DRR0H3svO_kLA03A2wGD2A7ayw6_onXSBPiABjF20gXBv2VkFRwHpQ5YDW342CpTceWmwNG765X0UeGkldRSt-Frw5WLUTTv8AuvFF78fTAaCdtcUUguOB1Ox1ATK0NpwWwqxaPzq3qqRUiLtTdxy0CUHeN488fmuM_6k87kod1s2g2EsZg9nHRj4ILGhFzKIJ07yW7JPpia-6haKqljsmW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