<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/82516>82516</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[X86] Improve KnownBits for X86ISD::HADD/HSUB nodes
</td>
</tr>
<tr>
<th>Labels</th>
<td>
backend:X86
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
RKSimon
</td>
</tr>
</table>
<pre>
https://gcc.godbolt.org/z/MMs368qzh
```ll
define <8 x i16> @add_trunc(<8 x i32> %x, <8 x i32> %y) {
entry:
%0 = and <8 x i32> %x, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%1 = and <8 x i32> %y, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%add.i = add nuw nsw <8 x i32> %1, %0
%conv = trunc <8 x i32> %add.i to <8 x i16>
ret <8 x i16> %conv
}
define <8 x i16> @hadd_trunc(<8 x i32> %x, <8 x i32> %y) {
entry:
%0 = and <8 x i32> %x, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%1 = and <8 x i32> %y, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%2 = tail call <8 x i32> @llvm.x86.avx2.phadd.d(<8 x i32> %0, <8 x i32> %1)
%conv = trunc <8 x i32> %2 to <8 x i16>
ret <8 x i16> %conv
}
declare <8 x i32> @llvm.x86.avx2.phadd.d(<8 x i32>, <8 x i32>)
```
```asm
add_trunc(int vector[8], int vector[8]): # @add_trunc(int vector[8], int vector[8])
vpbroadcastd .LCPI0_0(%rip), %ymm2 # ymm2 = [3,3,3,3,3,3,3,3]
vpand %ymm2, %ymm0, %ymm0
vpand %ymm2, %ymm1, %ymm1
vpaddd %ymm0, %ymm1, %ymm0
vextracti128 $1, %ymm0, %xmm1
vpackusdw %xmm1, %xmm0, %xmm0
vzeroupper
retq
hadd_trunc(int vector[8], int vector[8]): # @hadd_trunc(int vector[8], int vector[8])
vpbroadcastd .LCPI2_0(%rip), %ymm2 # ymm2 = [3,3,3,3,3,3,3,3]
vpand %ymm2, %ymm0, %ymm0
vpand %ymm2, %ymm1, %ymm1
vphaddd %ymm1, %ymm0, %ymm0
vpshufb .LCPI2_1(%rip), %ymm0, %ymm0 # ymm0 = ymm0[0,1,4,5,8,9,12,13,u,u,u,u,u,u,u,u,16,17,20,21,24,25,28,29,u,u,u,u,u,u,u,u]
vpermq $232, %ymm0, %ymm0 # ymm0 = ymm0[0,2,2,3]
vzeroupper
retq
```
We're failing to compute the knownbits of HADD/HSUB instructions, which prevents later instructions from making use of the known value range (in this case forcing more complicated truncations).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVtFu4ygU_RrycjWWfYkT-yEPTTLRVLMjrbZa7byNMOCYrW1cwGnar1-B0zSN05mqs_uw0lh1SoB7zuXcE4BZq7atlAuSLkm6nrDeVdos_vh8oxrdTgotHhaVc50l9IrghuBmy3m01aLQtYu02RLcPBLcfPli6Sy7e6xIvCbx1eFzFg9_dT10CFmqVgKhqwz2oJIZoR-BTGMmxDdn-pYTzJ4GKYZBTPcEVzDqfSCYA5kvB2DZOvPgUwzfwE-IgdA1sFaMY58QFUWgvv0zDfrxhDR5nfThPyNlQkRqIBYC2v4eWns_TiAJCWAan4Ry3e5CZFB_HDNAO_2yZE8ARrrzWg6Qh_LP198ve_Wr7j9DikPlmKqBs7o-p57Gdb1ron02i9huj1Hn5Y7EJanji1InBPMD3Rusgv-CTXjNjHznOkZLOGZ_3IbOvjLbDD2nNlStg53kThuSLjOSroP4486cUC8LPd--3h5_UGd4dl1hNBOcWScg-m31-3X8LfZLxNSozk8ffrwPTYMAgTg0fUFIuvQWef1N1-dk3qnwhPcMHZ823xaSnDbPQ4QQMIZOXmeRe2cYdyrBDALbNBlnt79ExW97K-6Pw8eZ8WnzZdCjNLrvOmkO_Ua6u9Pjq3q3L0bPwSjvRPyB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