<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/154116>154116</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
separate-const-offset-from-gep miscompile related to "trunc nuw"
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
mikaelholmen
</td>
</tr>
</table>
<pre>
llvm commit: 673750feea
Reproduce with:
```
opt -passes=separate-const-offset-from-gep bbi-109669_sep.ll -S -o - -mtriple=hexagon
```
The input program does
```
%0 = phi i32 [ -20, %entry ]
%1 = add i32 %0, 20
%2 = trunc nuw i32 %1 to i16
```
where the "trunc nuw" is ok since %1 is 0. No truncated bits are set.
However, in the result we get:
```
%0 = phi i32 [ -20, %entry ]
[...]
%1 = trunc nuw i32 %0 to i16
```
so here the input to "trunc nuw" is negative and the result is poison.
Langref https://llvm.org/docs/LangRef.html#trunc-to-instruction says
"If the nuw keyword is present, and any of the truncated bits are non-zero, the result is a [poison value](https://llvm.org/docs/LangRef.html#poisonvalues)."
Then %1 is further used to compute the return value from the function so the whole thing results in poison which it did not in the input.
(I stumbled upon this as a miscompile after
https://github.com/llvm/llvm-project/pull/152990
which I first incorrectly blamed.)
[bbi-109669_sep.ll.gz](https://github.com/user-attachments/files/21836248/bbi-109669_sep.ll.gz)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVF1vqzgT_jXOzQhkhkDggov2jaK30movzp77lQMDeGtsZA_N5vz6lYF2z2krrSpFioDHM8_HeFQIerBEjSgeRXE-qIVH55tJPysyozMT2cPVdffGmJcJWjdNmkX-AOUpPxWyJ1JCPgj58I1m77qlJbhpHkUe34lS7j_54GaGZFYhUBD5OdCsvGJKWmcDJ67vA3HSezclA81wveokk3VZ1n8GmlNjIPkDEgcJJBN7PRsS-Xmkv9Xg7Ls-30cCbeeFYfZu8GqCzlF4BxJYSBD5G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