<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/80149>80149</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Compare result flags sometimes not reused. (Missed optimization)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Explorer09
</td>
</tr>
</table>
<pre>
This issues can be reproduced in Compiler Explorer (godbolt.org). It affects at least x86-64 and ARM64 Clang targets.
## Test code ##
```c
#include <stdio.h>
void func1(int x, int y) {
if ((x - y) > 0)
putchar('>');
if ((x - y) < 0)
putchar('<');
}
void func2(int x, int y) {
if (x > y)
putchar('>');
if (x < y)
putchar('<');
}
void func3(int x, int y) {
int diff = x - y;
if (diff > 0)
putchar('>');
if (x < y)
putchar('<');
}
void func2_const(int x) {
if (x > 10)
putchar('>');
if (x < 10)
putchar('<');
}
```
## Actual result ##
x86-64 clang with "-Os" mode produces the following.
In short, the "cmp" instruction is generated twice in `func2()` with two redundant "mov" instructions. Similar issue happens with `func3()` too. Unlike `func1()` and `func2_const()` where clang knows the compare result flags can be reused.
(The assembly of `func3()` is omitted but it's almost identical to `func2()`)
```x86asm
func1:
movl %edi, %eax
movl $62, %edi
subl %esi, %eax
jg putchar@PLT # TAILCALL
js .LBB0_3
retq
.LBB0_3:
movl $60, %edi
jmp putchar@PLT # TAILCALL
func2:
pushq %rbp
pushq %rbx
pushq %rax
movl %esi, %ebx
movl %edi, %ebp
cmpl %esi, %edi
jle .LBB1_2
movl $62, %edi
callq putchar@PLT
.LBB1_2:
cmpl %ebx, %ebp
jge .LBB1_3
movl $60, %edi
addq $8, %rsp
popq %rbx
popq %rbp
jmp putchar@PLT # TAILCALL
.LBB1_3:
addq $8, %rsp
popq %rbx
popq %rbp
retq
func2_const:
movl %edi, %eax
movl $62, %edi
cmpl $10, %eax
jg putchar@PLT # TAILCALL
movl $60, %edi
jne putchar@PLT # TAILCALL
retq
```
## Expected result ##
`func1()`, `func2()` and `func3()` should generate identical assembly.
(While it's not part of this issue, the "sub" instruction generated in `func1()` may be replaced with a "cmp".)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysV1FvozgQ_jXOy6gRGELIQx7StJVW6upOdz3d48rYBtw1NrVNk96vPzlAIDTJZlcbRYCYYfzNN575gFgrCsX5Gi3u0eJhRhpXarN-3NdSG26C1SzT7GP9UgoLwtqGW6BEQcbB8Npo1lDOQCjY6qoWkhvonwSE00KzTEs316ZAeDWHLw5InnPqLBAHkhPrYJ8md0kMRDHY_PU1iWEriSrAEVNwZ-coeEDBpjviCOEIXrh1QDXj0N7ojEnQ_unRWSgqG-8Wba1jQs9LFD221nctGOSNoiHCqVAO9ghvwV98ILwCtLxv_QAARO5zQTjdw11njh4hQHg1-Phf3ThaEnPwXfqV8NL7RH2oc3G2P46zncRBy4cxKcdM8K2Z7A_4P34a_0mA7TjALyKObkCsHDCR54CiB2h5-4ync_iVmvz2nPA3qpV1Q2ZXShAGF5f7EdArj15G2nfImZ7aUNcQCYbbRrrTvjocuy6lh97cCVcCwvjuD4swhsq3YjcLLLiSQ66l1DuhipP2_aLAlto4X23vhTCmVe0jCGWdaagTWoGwUHDFDXGcgdsJyv14QUnQ73GfWxK0INxOg-GsUYwoDxtX-n0S0M7hb1EJSUw7v6Akdc2V7bJo40ZDXKf1HP5RUnznvTUcrH5M9ViOle4RldzwjqLvSu9aLqiuamJ4T20uSTEaoY3lbDLj0peSA7GWV5n8AJ2fwSgs6Eo4z1DWOBAO4aUFIittHQjGlROUSHD6M2_HbTPZE_s0IbZqb7Y5R5vTVqr0u_RnhBecCV9Ef0X2l7ziBPdOTJw62SY7hrIXQ70Wx60dB38-v8Bh-G--PG83z88TV9ue58_398G3bueC4e6tvezvX84p9tScR_ta1Sez5BKYlujpEnVjy7c2V5PVV2z7YzsPNy-zOyYuu-I1VGq6OK3qM7E-JS_5kdnwGz6z0NVK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