<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/81375>81375</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            aarch64: reduction of compare result produces not so good code
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          pinskia
      </td>
    </tr>
</table>

<pre>
    Take:
```
void f(unsigned int * __restrict a, unsigned int * __restrict b)
{
  unsigned int t = 0;
 t += -(a[0] == b[0]);
  t += -(a[1] == b[1]);
  t += -(a[2] == b[2]);
  t += -(a[3] == b[3]);
  a+=4; b+=4;
  *a = t;
}
```

LLVM produces currently:
```
f:
        ldr q0, [x1]
        ldr     q1, [x0]
        adrp    x8, .LCPI0_0
 cmeq    v0.4s, v1.4s, v0.4s
        ldr     q1, [x8, :lo12:.LCPI0_0]
        and     v0.16b, v0.16b, v1.16b
        addv s0, v0.4s
        fmov    w8, s0
        and     w8, w8, #0xf
 fmov    s0, w8
        cnt     v0.8b, v0.8b
        uaddlv  h0, v0.8b
        fmov    w8, s0
        neg     w8, w8
        str w8, [x0, #16]
        ret
```
Where LCPI0_0 is {1,2,4,8}.
What is it is trying to do is make a bit mask for the comparison and then count how many bits are set.
Why not instead just do:
```
f:
        ldr q0, [x1]
        ldr     q1, [x0]
        cmeq    v0.4s, v1.4s, v0.4s
        addv    s0, v0.4s
        str     s0, [x0, #16]
 ret
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vc1u8ygUfZrrzVUjjP8XXiSNIo3UkWYxmllW2OCYrzakgNPm7UcQO1WTptVsPivi755z74FDbGat3Cshasg2kG0jNrlem_oglX2RLGo0P9V_sxcByRrIFsgacjL_wvSoJccOaDmpkIijVA6BrvH52QjrjGwdMqCP-B2gAVrN6YvNeYCfCQ4h2SKBZAn7HBu_9gC0ZJBtCGRbD_JrzTz3aS-MW0p8RYl_ptArCv2ZklxRkhsKOzNSSDb-KJbxEga6ZmH77rIIxfZLO87t09M_f-LBaD61wmI7GSOUG073POwuAZyfgRt8Jd41yDbv4Vhu4v55jRcMucEwbg6-fy89ZvX0-Ncf5HmuiO0oXn3wSFap9fFjvAzCyg_VQkpI1oOOKSTrS_IbDYrjXCbOmzn9MorD6EozP6Ild3R0oz76_i2Ut-TrWufouQWakPduxi30c_638jO9VW6RWi5Kyyt5E-N8OCL25A7ge4VK7D8r_BS1ziyqg59n-XF-c6hGuC-v0b-9MAJnK1BahGLjHaNAH1OgjyUU29UCZc4jZGidOUm1R6eRaz8f2YtAho10ODL7gp026HqBrR4PzEirVThs1wuFrZ6Uw16_4cjUyXMsMiPQCnepdUKlHUplnWAcf03WIde_98_wv-57uIWXi_IFwFv1Abhn161PEa8TXiUVi0QdF6TIqqSgVdTXeVW1VUGbIk-zNE0ykcVl0XYkb4s8b1gVyZoSmhIak7hKMlqtGpE2JclzUhKSio5CSsTI5LAahuO40mYfSWsnUZdxUmTRwBox2PCNoVSJNwxBoP7tGZnacx6aaW8hJYO0zn5kcdINombMtH2eQuJ3xafWSa1Qd_ONEGiEnQb38crzfluNe605tpqLaDJD3Tt3sN5dugO620vXT82q1SPQnS83dw8Ho3-J1gHdBZEW6C5s4r8AAAD__3Yv2dw">