<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/144736>144736</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DAGCombiner] trunc + zext on i64 to i32 folded into constant 0
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dlee992
</td>
</tr>
</table>
<pre>
### 💡 Example: Trunc + Zext in LLVM IR
The following function demonstrates a `trunc` followed by a `zext`:
```llvm
define i64 @bar(i64 %a) {
; CHECK-LABEL: bar:
; CHECK: ; %bb.0:
; CHECK-NEXT: ret 0
%trunc = trunc nuw nsw i64 %a to i32
%res = zext i32 %trunc to i64
ret i64 %res
}
```
Ideally, LLVM should emit code that preserves the lower 32 bits of %a and zeros out the upper 32 bits. However, in the latest LLVM, it appears this truncation + zero-extension is incorrectly optimized into a constant 0. Since it's a new feature, I am not sure what's the correct semantics of nuw/nsw in trunc instruction.
This behavior is likely a regression and may be closely related to the following patch:
👉 llvm/llvm-project#113808
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxkVF1vgzYU_TXOy1UjYwKEBx5I26zVuj1s1TTtzeCb4M3YyL7ko79-siHqPiKkgH187vG5x5Yh6LNFbFhxYMXLRs40ON8og1jXYtM5dW-YyJcH2JGz-shqwdoMXm9ynAyyvIVPP9semDjAH3gj0BY-Pn77Cd5_YbxlvP0cEE7OGHfV9gyn2faknQWFo7OBvCQMIIGVnCIPK_mKRgXdfZn5whuxkrO8XSjje3qMuYyMtwpP2iLocgdsxzvpmdinD1FIJmpg1SGuyg_w_Pb6_OPTR3t4_YjSI3QhfczF0eUXh5goum7L_4N5-vn1988V6JGAM95CxNLiRP4Cy5udr2DDFR5agBzoXDzgHkMCfyXbcvFNEXHlLuFigXW9xxBlVC__tIDx9l2hNObOxPPifBjcbBTgqAl6pxBokASTx4D-ggFoQIgGe8gFdJoCuNOiT1oFX-hdADdTws3T9I3bwpu74gV9LKXtQhQbSKlwGiWQ04TSxzI6LEbI1PGYkEj-hDdCG-KQDqBt77zHnswd3ER61F-oQFtyIKGPEZGWgG_hV217BE1MVDEwFq9wQkmzx1j3HeQI1hGE2SNcB7ngosKVHwKO0pLu03btfGXimJpj127pGMc5pXP7iK4O0OEgL9r5KNbov9DEUHo8ewxpD9GzUd6hQ-iNC3HeY3RFxTbSv9I_SeqHNU3racrYvoaUY3GMf0-Td39iT0zkWZbv-X6jmlzVeS032GRVwUUpMiE2Q6N4lislUYpuX2GFXX0qygorUe3zssr4RjeCi4KX2T4rikKILS9U1dcFV_t9lalKsh3HUWqzjYW3zp83OoQZm2y3q_JyY2SHJqTLQYjod5plQsS7wjdJbTefA9txowOFbxrSZNKt8tL-8OzGTlv0rHgciyUIN4KYgHK3nopok3q0_rvxm9mbZiCaQvRNHJk4njUNc7ft3cOz_1l3TEoDE8d1K5dG_B0AAP__f6yJEA">