<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/85368>85368</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DSE] Missed optimization: skip `store(phi)` if the tautological value in phi is assigned
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
XChy
</td>
</tr>
</table>
<pre>
Alive2 proof: https://alive2.llvm.org/ce/z/JRtC28
### Motivating example
```llvm
define i32 @src(ptr %outl, i1 %c1, i1 %c2) {
entry:
store i32 0, ptr %outl, align 4
br i1 %c1, label %if.then, label %if.end
if.then: ; preds = %entry
call void @dummy()
br i1 %c2, label %common.ret, label %if.end
if.end: ; preds = %entry, %if.then
%storemerge = phi i32 [ 1, %if.then ], [ 0, %entry ]
store i32 %storemerge, ptr %outl, align 4
br label %common.ret
common.ret: ; preds = %if.then, %if.end
ret i32 0
}
```
can be transformed to:
```llvm
define i32 @tgt(ptr %outl, i1 %c1, i1 %c2) {
entry:
store i32 0, ptr %outl, align 4
br i1 %c1, label %if.then, label %common.ret
if.then: ; preds = %entry
call void @dummy()
br i1 %c2, label %common.ret, label %if.end
if.end: ; preds = %if.then
store i32 1, ptr %outl, align 4
br label %common.ret
common.ret: ; preds = %entry, %if.end, %if.then
ret i32 0
}
```
Notice that #75744 has fixed similar cases, I tag DSE here. But this case would require threading the basicblock.
### Real-world motivation
This snippet of IR is derived from [openssl/crypto/evp/evp_enc.c@EVP_DecryptFinal_ex](https://github.com/openssl/openssl/blob/f08be096517f9bdae8a9d1d837748237db4d13a9/crypto/evp/evp_enc.c#L940) (after O3 pipeline).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also:https://godbolt.org/z/aEbMeM8rP
**Let me know if you can confirm that it's an optimization opportunity, thanks.**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcVl1vozgU_TXOy9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