<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/86920>86920</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[DSE] Missed optimization : eliminate `store C, ptr` if `load ptr` is known to be `C`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
missed-optimization
</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/nVPFem
### Motivating example
```llvm
define void @src(ptr %p) {
entry:
%l1 = load i32, ptr %p, align 4
%cmp1 = icmp eq i32 %l1, 0
br i1 %cmp1, label %then, label %else
else: ; preds = %entry
call void @dummy()
%l2 = load i32, ptr %p, align 4
%cmp2 = icmp eq i32 %l2, 0
br i1 %cmp2, label %then, label %common.ret
then: ; preds = %entry, %else
store i32 0, ptr %p, align 4
br label %common.ret
common.ret: ; preds = %else, %then
ret void
}
```
can be folded to:
```llvm
define void @tgt(ptr %p) {
entry:
%l1 = load i32, ptr %p, align 4
%cmp1 = icmp eq i32 %l1, 0
br i1 %cmp1, label %then, label %else
else: ; preds = %entry
call void @dummy()
%l2 = load i32, ptr %p, align 4
%cmp2 = icmp eq i32 %l2, 0
br i1 %cmp2, label %then, label %common.ret
then: ; preds = %entry, %else
br label %common.ret
common.ret: ; preds = %else, %then
ret void
}
```
### Real-world motivation
This snippet of IR is derived from [linux/kernel/delayacct.c@delayacct_init](https://github.com/torvalds/linux/blob/8d025e2092e29bfd13e56c78e22af25fac83c8ec/kernel/delayacct.c#L39) (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/csqq89bef
**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/eJzsVs1u4zYQfhr6MohBjSxbOvjg2BWwwC5abIuit4IiRzYbilRIyrvepy8o2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