<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64304>64304</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang is suboptimal for (-a >> b) and/or (~a >> b)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
k-arrows
</td>
</tr>
</table>
<pre>
Consider the following two functions:
```c
int foo(int a, int b)
{
return (-a >> b) & (~a >> b);
}
int bar(int a, int b)
{
return (-a & ~a) >> b;
}
```
Clang vs GCC:
https://godbolt.org/z/5zKfET74h
Alive2:
https://alive2.llvm.org/ce/z/LrkarC
```llvm
define i32 @src(i32 %0, i32 %1) {
%2:
%3 = sub i32 0, %0
%4 = ashr i32 %3, %1
%5 = ashr i32 %0, %1
%6 = xor i32 %5, 4294967295
%7 = and i32 %4, %6
ret i32 %7
}
=>
define i32 @tgt(i32 %0, i32 %1) {
%2:
%3 = sub i32 0, %0
%4 = xor i32 %0, 4294967295
%5 = and i32 %3, %4
%6 = ashr i32 %5, %1
ret i32 %6
}
Transformation seems to be correct!
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VMFypCwQfhq8UDOFDehw8JDMxP_w7zEvgIrKhpEUYLKbQ559SxwnxkwOe9gqq2zor7_u_qCR3utuUKpA_B7xUyLH0FtXPO2kc_bVJ5VtfhdHO3jdKIdDr3BrjbGveuhweLW4HYc6aDt4RO8QOSFyhzIyf_W81kPArbUIDpMlERzxZFQIxCUgv58N7FQY3YARHHYSI_qA6EPEYQTZtPv-aRfR-4XgdDGuCSvp_j4hZPhdxnRLkq8ZlubWCY9GDh1-8fi_4_EqQx_CcxQFSgRlZ5vKmrC3rkNQviEo-dv_7cNjzvo1053RLwq-4ZDRuTfm5XzhqdWF7Id7ku64KXECzluNavWgsKaAESPe1ZM20wI4ifLMdhp7X8RBwD8qwZOfYkRP2I9VDIiRkeEDwSJC-t4tnPSCSlco_gVFbqCyiPplryA-gRgIJrIcBF9B85lwaBYou_BlC8ipsPjy7ZHS03TeN4QKXfi3Qq2aI982x7fNLZKyL2KtJeVbSVcSZBsJHp0cfGvdWU6jjL1SZ4-DxZXCtXVO1QFBurldSVPQRlAhE1WkmSAHkQomkr4QbZXznFHVSkUEqVIpK05aDoeWgWhoogsgQMmBpCmkAGLPieBcpVJUQh0kB8SIOkttrlc90d6PqsgYJSwxslLGL--VKybQrho7jxgx2gf_ERZ0MKqo43hqPx2IfQ76LA1urbvxzMihQVDOrs9vTTI6U2xGWod-rPa1PSMo46zNv92zsz-jZGUs2yMoY-V_AgAA___1KmhF">