<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/94737>94737</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[InstCombine] Failure to simplify average of lsb
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:instcombine,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
RKSimon
</td>
</tr>
</table>
<pre>
Noticed while investigating avgfloor codegen as part of #53648:
```c
unsigned src(unsigned a, unsigned b) {
return ((a % 2) + (b % 2)) / 2;
}
unsigned tgt(unsigned a, unsigned b) {
return (a & b & 1);
}
```
We fail to recognize that this can be treated as a basic logic op:
```ll
define i32 @src(i32 %a, i32 %b) {
%rem = and i32 %a, 1
%rem1 = and i32 %b, 1
%add = add nuw nsw i32 %rem1, %rem
%div2 = lshr i32 %add, 1
ret i32 %div2
}
define i32 @tgt(i32 %a, i32 %b) {
%and = and i32 %a, 1
%and1 = and i32 %and, %b
ret i32 %and1
}
```
Alive2: https://alive2.llvm.org/ce/z/XtJ4Rs
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVE2PpDYQ_TXFpTQjKL4PHHqmhZREymFzSK42NlCRsVu26dHOr4-A3s5OT5RdqdW4yu_51StDiRB4slp3UL5AeU7EGmfnuy-__cGLs4l06mv3u4s8aIVvMxuNbK86RJ5EZDuhuE6jcc7j4JSetEUR8CJ8RDciUF7mVdFAfoL0DOkJqvT4DUe82l1cYfADUHMPBdAr3iMJ1CLULwcH0eu4eotADVAjEKhE2iH0siXlPXMkeyTIb2Sozw_KcYo_r_y99iZcodz_s03sk8bd7RH-qXEUbDA69Hpwk-V3jXEWEePMAQdhUWqMXou4FRJQoBSBBzRu4gHd5XMbjTkSSo9sNXJOCEV6NHMPqNwN3daPdoBKrxeE_IzCKvyekX3EZI8g-QgSSh0YpdCub2jD2zfsxt_gx_LGASoVX2nnmDD7u7pSH472On7b2vAPLf5o_LjLnzO-efmRcWHVJ-PCqpsX-R8lboz_fwtOhq-aID_hHOMlbHdKPVAv9vyzMdfl2fkJqB80UP8O1P8Vfy2-hER1uWrzViS6y-qsrsssa9tk7sa2zMaxVlnT5kXTpkVeVaVQtWj0OIqxTLijlIq0Suu0Tau8fZbNUKS6VUIUsiplDkWqF8Hmrp5wCKvu2qLO68QIqU3Y5wPRhoD8xDbEwS2SrQYioFcgWjgErZ7cJfLC7yKys9teeU58t9Ge5DoFKFLDIYZ_pSJHs0-fX2yIr7czyzP2gs3q9fa5BF4uhsevKK7ai0lvs8UEicnqTfexjRPHeZXPg1uA-r3Y4_F08e5vPUSgfvcWgPrD3rWjfwIAAP__kj5q3w">