<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/85078>85078</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DebugInfo][DivRemPairs] Wrong branch execution caused by hoisting an 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>
This bug caused by hoisting the instruction `DivInst` to the conditional predecessor without dropping its debug location. Here is the [buggy line](https://github.com/llvm/llvm-project/blob/9fa866020395b215ece6140c2fedc7c31950272c/llvm/lib/Transforms/Scalar/DivRemPairs.cpp#L298).
Please check this @SLTozer @dwblaikie . If this is confirmed, I would like to give the patch to it.
```C
int func(int a, int b) {
int rem = a % b;
int ret = 3;
if (rem == 42) {
ret = a / b;
}
return ret;
}
int main() {
func(4, 2);
}
```
```Bash
$ clang -S -emit-llvm -Xclang -disable-O0-optnone main.c -o main.ll -g
$ opt -S -passes=mem2reg,div-rem-pairs -debug main.ll -o main-opt.ll
$ clang main-opt.ll
```
```Bash
$ mylldb a.out
(lldb) target create "a.out"
Current executable set to '/data1/llvm-test/DivRemPairs/a.out' (x86_64).
(lldb) b func
Breakpoint 1: where = a.out`func + 9 at main.c:2:17, address = 0x0000000000001139
(lldb) r
Process 2794232 launched: '/data1/llvm-test/DivRemPairs/a.out' (x86_64)
Process 2794232 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
frame #0: 0x0000555555555139 a.out`func(a=4, b=2) at main.c:2:17
1 int func(int a, int b) {
-> 2 int rem = a % b;
3 int ret = 3;
4 if (rem == 42) {
5 ret = a / b;
6 }
7 return ret;
(lldb) s
Process 2794232 stopped
* thread #1, name = 'a.out', stop reason = step in
frame #0: 0x0000555555555142 a.out`func(a=4, b=2) at main.c:5:17
2 int rem = a % b;
3 int ret = 3;
4 if (rem == 42) {
-> 5 ret = a / b;
6 }
7 return ret;
8 }
(lldb) v rem
(int) rem = 0
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVs1u4zYQfhr6MrAhDSVLOvhgRzUaYIEuugu0t4KixhIbihRIKtn06QvKdmKv0262RVEiiOnhzMf5_UzhveoM0YblO5bXCzGF3rrNdrSyJ-MXjW2fN5975aGZOpBi8tRC8wy9VT4o00HoCZTxwU0yKGuArZNaPd4bH9g6gWBnBWlNq-Kx0DA6akmS99bBkwq9nQK0zo5jRFPBQ0vxKm2liBYr-JEcgfIzEMt3zdR1z6CVIZbXDMs-hNEzvmW4Z7jvVOinZiXtwHCv9eP5Yzk6-zvJwHDfaNsw3FcHUa7XCSa8yhtMc5K0TrNE4oFaWUieVnmCBcoLHBXtPjth_MG6wTPcf5JCC8dwX6vHn2n4KJTzKzmODPkHrEqG1YolNUu2x_8fNQlPIHuSDxBiVlmWfPrw2f5BLm7bp0YL9aAIVnB_OGooH9N3UG6gluEd3MOTnXQLWj1QzG-nHmnOzSiC7KNEhatL2To5_t0dvysT4DAZybCMWxFB46ZhWAErdkctAJiljgZgvAYBDHNoGL85D_M5vz45AMPyZBuPM7xBj-tsHdH3l-isqOFV1VGYnIkfLxqsqC-DjK4MQhmG5c1Fp2CzGGh04w2Mc4q--roTvj-JMAOphelg-QmWNKiwjF0By19P0lZ50Wha_pQs7RiMNTT7s5KwtMed1rDsXtHsGGasUXhPnvF6oAEddQzvWvW4dDQsx9hOsDwOxAvGES7estL6a-9uj94V2vCsdduAWMVpPIvLKIvZDMJ1FEA6EoGAIc56DPGoeTc5RyYAfSE5hZgF8BRiJzIsGO5bEUR6nsNAPlzPC8P9Ca-ITfOlXP-2zi4m59WP5ljKWbxzJB5GG-ueMr6Fpz7SxNxLM9g6ibrAcAcViHAqBuNbZHybFrEXRNs68n42Sr4kFytNeXVzuzvNsLORvQCLKkOOoMVkZE9tdOJfxfs2vA92HKk9e7OF0DsSLTDkaQzCiOEYNsPiDBvl0Q4cCR85mdfQX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