<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64035>64035</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LICM] Missed optimization: 200/var_1
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ZY546
</td>
</tr>
</table>
<pre>
Test code: https://godbolt.org/z/sjjoq8v63
```c++
int var_19,var_30;
void test(int var_0, int var_1, int var_2, int var_3) {
while(var_3){
if(var_0){
var_30 += var_1 + var_2;
}
else{
var_19+=200/var_1;
}
var_3++;
}
}
```
The expression `200/var_1` is a loop invariant, but under the optimization at the O2 level, the resulting code still evaluates each time the execution reaches the else branch. Is it an optimization missed by `LICM Pass`?
```asm
.LBB0_3: # %while.body.prol
test edi, edi
je .LBB0_5
add r10d, r8d
mov dword ptr [rip + var_30], r10d
jmp .LBB0_6
.LBB0_5: # %if.else.prol
mov eax, 200
xor edx, edx
idiv esi
add r9d, eax
mov dword ptr [rip + var_19], r9d
.LBB0_6: # %if.end.prol
lea r11d, [rcx + 1]
cmp ecx, -1
jne .LBB0_8
jmp .LBB0_15
.LBB0_13: # %if.else.1
mov eax, 200
xor edx, edx
idiv esi
add r9d, eax
mov dword ptr [rip + var_19], r9d
add r11d, 2
je .LBB0_15
.LBB0_8: # %while.body
test edi, edi
je .LBB0_10
add r10d, r8d
mov dword ptr [rip + var_30], r10d
test edi, edi
jne .LBB0_12
jmp .LBB0_13
.LBB0_10: # %if.else
mov eax, 200
xor edx, edx
idiv esi
add r9d, eax
mov dword ptr [rip + var_19], r9d
test edi, edi
je .LBB0_13
.LBB0_12: # %if.then.1
add r10d, r8d
mov dword ptr [rip + var_30], r10d
add r11d, 2
jne .LBB0_8
.LBB0_15: # %while.end
ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVk2P4ygQ_TXkUpoIg3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