<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/78281>78281</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SimplifyCFG] eliminateDeadSwitchCases does not eliminate dead branches.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:optimizations,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
DianQK
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
DianQK
</td>
</tr>
</table>
<pre>
I tried the following code, which I expect to be optimized.
```llvm
define i32 @src(i32 noundef %0) {
%2 = icmp eq i32 %0, 0
%3 = icmp eq i32 %0, 1
%foo = zext i1 %3 to i32
%bar = select i1 %2, i32 -1, i32 %foo
switch i32 %bar, label %4 [
i32 0, label %7
i32 -1, label %7
]
4: ; preds = %1
%5 = icmp eq i32 %0, 2
%6 = zext i1 %5 to i32
br label %7
7: ; preds = %4, %1, %1
%8 = phi i32 [ %6, %4 ], [ 1, %1 ], [ 1, %1 ]
ret i32 %8
}
define i32 @tgt(i32 noundef %0) {
%.not = icmp ne i32 %0, 1
%2 = zext i1 %.not to i32
ret i32 %2
}
```
Alive2: https://alive2.llvm.org/ce/z/zyMuXt
Similar to #73446. But it's more complicated.
This should be caused by KnownBits.
We know that this `%bar` can only be -1, 0 and 1. So we can transform the default branch to a branch with a specific value.
There are several variants of this issue, such as replacing -1 with some other value and increasing the number of cases.
From: https://github.com/rust-lang/rust/issues/119520.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVUGPqzYQ_jXOZRQEBkJy4LBJlOrpqYdqK7VXYw_BrbGpbZKX_fWVDcmS3bettAvG_mbmmy8zY-acPGvEmpR7QulRMv3bd0IpKY8rNvrO2HraWzVG3Opv4K1EAb5DaI1S5ir1GbgRSOgBrp3kHXwD_DEg9-ANNAhm8LKXbygSkh5J-jI_N-n0p9Sln7YEtlIjyJwCKVJnOaHb8KHNqAW2QGiZEroDUu0nAwhbF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