<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/142744>142744</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization of constant folding when using 'trunc nuw i64 %v to i1'
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
GINN-Imp
</td>
</tr>
</table>
<pre>
The following reduced IR is derived from https://github.com/rust-lang/regex/blob/1a069b9232c607b34c4937122361aa075ef573fa/regex-automata/src/util/pool.rs#L542.
Godbolt: https://godbolt.org/z/KebcfWbE8 (contains the source code before manual reduce)
alive2 proof: https://alive2.llvm.org/ce/z/XPqJVm
Missed optimization: `store i64 %v, ptr %p, align 8` --> `store i64 0, ptr %p, align 8`
While Alive2 proves this transformation to be correct, I’m not fully certain why this optimization is valid from a semantic standpoint — especially regarding how trunc nuw i64 %v to i1 constrains %v. Any clarification on why this optimization is valid would be appreciated.
```llvm
define i64 @src(ptr %p, i64 %v) {
%trunc = trunc nuw i64 %v to i1
br i1 %trunc, label %common.ret, label %3
common.ret: ; preds = %1, %3
ret i64 0
3: ; preds = %1
store i64 %v, ptr %p, align 8
br label %common.ret
}
```
expected:
```llvm
define i64 @tgt(ptr %p, i64 %v) {
%trunc = trunc nuw i64 %v to i1
br i1 %trunc, label %common.ret, label %3
common.ret: ; preds = %1, %3
ret i64 0
3: ; preds = %1
store i64 0, ptr %p, align 8
br label %common.ret
}
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsVU1v4zYQ_TX0ZWCDImnJOuigNPEi_VgURdHtlSJHFguKVEnK3uyvLyg52Wy72aTotUEA0_x482be84yM0ZwcYkP2N2R_u5FzGnxo3t2_f7-9H6dN5_VD8-uA0Htr_cW4EwTUs0IN97-AiaAxmDNq6IMfYUhpioS3hB0JO55MGuZup_xI2DHMMW2tdKe8xhN-JOzYWd8RdiwkL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