<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/87624>87624</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[ValueTracking] ComputeNumSignBitsImpl - add basic handling of BITCAST nodes
</td>
</tr>
<tr>
<th>Labels</th>
<td>
good first issue,
llvm:instcombine,
llvm:support
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
RKSimon
</td>
</tr>
</table>
<pre>
At the very least ComputeNumSignBitsImpl should be able to report that if we bitcast from a wider scalar/vector type to a narrow vector element type, and the original wide type was all sign bits then the destination narrow type elements will be all sign bits as well.
SelectionDAG already does this (and quite a bit more, but for the "fast case" would be enough): https://github.com/llvm/llvm-project/blob/5ad320abe36357e3290007d3ab353e8637f33720/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp#L4425-L4469
Encountered while fighting bitcasts between SSE intrinsics.
https://alive2.llvm.org/ce/z/NvZpPa
```
----------------------------------------
define <4 x i2> @src(<1 x i8> %a0, <1 x i8> %a1) {
#0:
%cmp = icmp sgt <1 x i8> %a0, %a1
%ext = sext <1 x i1> %cmp to <1 x i8>
%sub = bitcast <1 x i8> %ext to <4 x i2>
%sra = ashr <4 x i2> %sub, { 1, 1, 1, 1 }
ret <4 x i2> %sra
}
=>
define <4 x i2> @tgt(<1 x i8> %a0, <1 x i8> %a1) {
#0:
%cmp = icmp sgt <1 x i8> %a0, %a1
%ext = sext <1 x i1> %cmp to <1 x i8>
%sub = bitcast <1 x i8> %ext to <4 x i2>
ret <4 x i2> %sub
}
Transformation seems to be correct!
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzklc-P4jYUx_8ac3kaZJ4TQg4cGJisVl2tqjLqoTcneUncOnZqO8NO__rKBmYZpiu15yIUbOd9vu-H8bP0XvWGaMvyR5YfFnIOg3XbX346qtGaRW3b1-0uQBgIXsi9gibpA-ztOM2Bvs7jUf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