<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/95343>95343</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DebugInfo][Reassociate] Missing debug location drop for the moved instruction
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Apochens
</td>
</tr>
</table>
<pre>
Missing debug location drop for the moved [`TheNeg`](https://github.com/llvm/llvm-project/blob/78ee473784e5ef6f0b19ce4cb111fb6e4d23c6b2/llvm/lib/Transforms/Scalar/Reassociate.cpp#L847).
Here is the testcase (main.c):
```c
void func(int a, int c, int *ptr) {
int d = 1;
while (c) {
int sub1 = a - d;
int dead1 = sub1 + 1;
int dead2 = dead1 * 3;
int dead3 = dead2 * sub1;
int sub2 = 0 - d;
*ptr = sub2;
d = 2;
}
}
int main() {
int p = 0;
func(12, 0, &p);
}
```
Compilation:
```
$ clang -S -emit-llvm -Xclang -disable-O0-optnone main.c -o main.ll
$ opt -S -passes=mem2reg,reassociate main.ll -o opted.ll
$ clang opted.ll
```
Debugging:
```
$ lldb a.out
(lldb) b func
(lldb) r
(lldb) s
Process 35731 stopped
* thread #1, name = 'a.out', stop reason = step in
frame #0: 0x000055555555514e a.out`func(a=12, c=0, ptr=0x00007fffffffe14c) at main.c:8:22
5 int dead1 = sub1 + 1;
6 int dead2 = dead1 * 3;
7 int dead3 = dead2 * sub1;
-> 8 int sub2 = 0 - d;
9 *ptr = sub2;
10 d = 2;
11 }
(lldb) v c
(int) c = 0
```
Root cause: Moving the `%sub3` into the preheader of the loop without dropping its debug location
```
while.cond: ; preds = %while.body, %entry
%d.0 = phi i32 [ 1, %entry ], [ 2, %while.body ], !dbg !17
->%sub3 = sub i32 0, %d.0, !dbg !22
%tobool = icmp ne i32 %c, 0, !dbg !21
br i1 %tobool, label %while.body, label %while.end, !dbg !21
while.body: ; preds = %while.cond
%sub = add i32 %a, 1, !dbg !26
%add = add i32 %sub, %sub3, !dbg !27
%mul = mul nsw i32 %add, 3, !dbg !29
store i32 %sub3, ptr %ptr, align 4, !dbg !33
br label %while.cond, !dbg !21, !llvm.loop !34
```
The regression tests in `llvm/test/` that can also reproduce this misleading debug location: `2003-08-12-InfiniteLoop.ll` and `pr28367.ll`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVs2O47ANfhrlQiSQKf8khxwykw66wG5b7O6hV1liYhWOZFjyTPftC8n2xMlkp1tUCGKb4keKfyKl9-ZsifaseGLFcSWH0Lh-f-icasj6Ve30r_03472xZ9BUD2donZLBOAu6dx2cXA-hIbi4V9IQpZT8Z0N_ozMrOSuODLdNCJ1n4sDwheHL2YRmqDfKXRi-tO3r_Fh3vfsXqcDwpW5dzfCl2hLllai2ORV0Kk-8znaKclVnWXaqS8o1ClXWuJBjIu5nL60_uf7iGb78ULKVPcOX7yS9d8rIQBvVdQzF121eMdxtGD8yfhj__0o9gfHJpEA-KOkJGG4v0tiNYriLdoz8JR9_avx-dUbDabCK4dbYAJLhM8QXNb8wPHShZ7gDVj2NoETXwMQRMiZmIgC8NaZNitUt_7QizA91lpAS1qBv0DOLJqlHnpEZn-7ULBkxMU4QPID4Lad458TEGYU_ZPZDPUrlD484OmQ-H173R4_gDYBVx8nx7y_pP-qJ0WG4_eCquNeNB7iRNYUpwxgaHv8Yll2K7tO9kjnMS53P7tKZNlXBx3yYPjEH1Up7hvUPWNPFhHVMUlj_c6Jq42Xd0vrvfO26YJ0lGJMM1m58a9urKNeFJKiT3pNn4nihC_Z0ZvjcXxN7xkURrgukb2SMiu_oj8w7xjo_G3v-zLi21TXIjRvCTNtGUoxBPTr4ntx_oPiR8o_eKfIeRFGJDHxwXUd6Zj5AaHqSGhiKLIbKygulmDKsRv1YRXrEQXSGs2NKBerA2EXU-4REwZk4AP8355wX88pymqwp-ZQekonjmCKKiWNKk1i_4jhCq9O4KMtTkcowBZCJw5a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