<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63876>63876</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SimplifyCFG] Switch not fully optimized
</td>
</tr>
<tr>
<th>Labels</th>
<td>
llvm:optimizations,
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
nikic
</td>
</tr>
</table>
<pre>
https://alive2.llvm.org/ce/z/vAWi88
```llvm
define i8 @src(i8 noundef %arg) {
start:
switch i8 %arg, label %unreachable [
i8 0, label %case012
i8 1, label %case1
i8 2, label %case2
i8 3, label %end
]
unreachable:
unreachable
case1:
br label %case012
case2:
br label %case012
case012:
%phi1 = phi i8 [ 3, %case2 ], [ 2, %case1 ], [ 1, %start ]
br label %end
end:
%phi2 = phi i8 [ %phi1, %case012 ], [ 4, %start ]
ret i8 %phi2
}
```
We should combine this into a single phi, resulting in something like this:
```llvm
define i8 @tgt(i8 noundef %arg) {
start:
switch i8 %arg, label %unreachable [
i8 0, label %end
i8 1, label %case1
i8 2, label %case2
i8 3, label %case3
]
unreachable:
unreachable
case1:
br label %end
case2:
br label %end
case3:
br label %end
end:
%phi = phi i8 [ 4, %case3 ], [ 3, %case2 ], [ 2, %case1 ], [ 1, %start ]
ret i8 %phi
}
```
At which point it is subject to lookup table optimization and converted into "add 1".
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEVd1uszgQfRpzM2oENr8XXCRfxD5AL3ptsBNma2yETar26VcG0kD6s93Vrj4pIhrmjOec4YzMrcWzlrIkyYEkx4CPrjVDqfEZm6A24rVsnestYXtCK0IrrvAi6U6pS7czw5nQqpGEVm-EVpf9E-Y5CY8k3JM0nH8eOL8S8oRaAuZA4tAODaE55qDNqIU8AaEJ98cVQLLDXGAdH5xvPEUA9gVd004HLOBfoHgtlY9HPUjetLxWEryUpQY8PNwgG25lGNENIvqAiDZ5-iG_rWebvNTimiXJcRnI9FyxvAlbv1xhZxY3-fXwhYYbnP4zuH91KyA06VuMgLAj9C1Oc04Os7Sr6EmPj5PDPJPrtNaJaElM3-82gS2l9xnNTx_eU6H3VBaGq75htKEUf9V5kG7xjT93aZwd77w6h08SbGtGJaAxXe0t61q0gNoZ4GBRn5X0rHyvQdpROdRnQA3WdNK1PlD4PFe9a_q7fXBn97v2YeXW_2EXfJ4t-Z8sw7_ZhjsvfbcJn0DZz6CfOfTeoPHKmmxtzP9wiTZW_t7JewcvLTYt9Aa1A3SAFuxY_ykbB86AMuZ57MFNJjG9ww7fuEOjgWvvfn2Rg5Nitj6hlAvhedFdIEomClbwQJZRmhcszaK8CNqySKIsFU2SxlFYR0XEw5QXWcJFVseSxSzAkoaUhVkU05AVcbFLctaIhHFZJKIWpxOJQ9lxVO83TIDWjrJMWZ6lwfRx7HRXUTqtEtuviVtC_UQJpR1aK8XDOulzyTEYSl_4UI9nS-JQoXX21syhU9Nd-Ihdr_D0-qv6gyRHeJx3TRsHp1Gp1-u4pAjGQd3dkWd07VjvGtMRWk0s57-HfjB-9oRWkyhLaDXp-isAAP__JLgZgw">