<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/143778>143778</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Unnecessary `and x,C` introduced due to assume on trunc+zext
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dzaima
</td>
</tr>
</table>
<pre>
The code:
```c
#include<stdint.h>
uint64_t foo(uint64_t a) {
uint32_t b = a;
if (b >= 100) __builtin_unreachable();
return b;
}
```
with `-O3` compiles to:
```asm
foo:
mov rax, rdi
and eax, 127
ret
```
but it could be:
```asm
foo:
mov eax, edi
ret
```
[compiler explorer](https://godbolt.org/z/5v3dY9nTr) on the instcombine pass that introduces the `and`; namely, it transforms:
```llvm
define dso_local range(i64 0, 4294967296) i64 @foo(i64 noundef %a) local_unnamed_addr {
entry:
%conv = trunc i64 %a to i32
%cmp = icmp uge i32 %conv, 100
%0 = xor i1 %cmp, true
call void @llvm.assume(i1 %0)
%conv2 = zext i32 %conv to i64
ret i64 %conv2
}
```
to:
```llvm
define dso_local range(i64 0, 4294967296) i64 @foo(i64 noundef %a) local_unnamed_addr {
entry:
%conv = trunc i64 %a to i32
%cmp = icmp ult i32 %conv, 100
call void @llvm.assume(i1 %cmp)
%conv2 = and i64 %a, 127
ret i64 %conv2
}
```
reduced from [this Rust code](https://godbolt.org/z/Kcq1qjoas)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUVctuI7kO_Rp5Q8RQqd6LWthJe3MXFxj0LGZlqEq0Sw2V5NbDneTrB5TttOHMo7cTBLAlkofk4REtQ9BHiziwesvql5VMcXZ-UO9SL3I1OvU2fJ0RJqeQlRvGN6zhl_-JDqLUdjKJjM8hKm3jemblFzLxTdI2NtU-wsE5JrqPo2SiB9ZuGd8AANB9KfYRRmDlC0hW3iz6AEx0dP2FLAXnFLnfj0mbqO0-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