<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/95919>95919</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SimplifyCFG] `ForwardSwitchConditionToPHI` missed some cases
</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 this IR:
```llvm
define { i64, i64 } @src(i64 noundef %arg, i64 noundef %arg1) local_unnamed_addr #0 {
bb:
switch i64 %arg1, label %bb7 [
i64 0, label %bb3
i64 1, label %bb2
]
bb2: ; preds = %bb
br label %bb3
bb7: ; preds = %bb
%i8 = udiv i64 %arg, %arg1
%i13 = shl nuw i64 %i8, 1
br label %bb3
bb3: ; preds = %bb7, %bb2, %bb
%i = phi i64 [ %i13, %bb7 ], [ %arg, %bb2 ], [ undef, %bb ]
%i4 = phi i64 [ 1, %bb7 ], [ 1, %bb2 ], [ %arg1, %bb ]
%i5 = insertvalue { i64, i64 } poison, i64 %i4, 0
%i6 = insertvalue { i64, i64 } %i5, i64 %i, 1
ret { i64, i64 } %i6
}
```
We can replace `[ 1, %bb2 ]` with `[ %arg1, %bb2 ]`. Then the final IR will become to:
```llvm
define { i64, i64 } @tgt(i64 noundef %arg, i64 noundef %arg1) {
bb:
%switch = icmp ult i64 %arg1, 2
br i1 %switch, label %bb3, label %bb7
bb7: ; preds = %bb
%i8 = udiv i64 %arg, %arg1
%i13 = shl nuw i64 %i8, 1
br label %bb3
bb3: ; preds = %bb, %bb7
%i = phi i64 [ %i13, %bb7 ], [ %arg, %bb ]
%i4 = phi i64 [ 1, %bb7 ], [ %arg1, %bb ]
%i5 = insertvalue { i64, i64 } poison, i64 %i4, 0
%i6 = insertvalue { i64, i64 } %i5, i64 %i, 1
ret { i64, i64 } %i6
}
```
Alive2: https://alive2.llvm.org/ce/z/HvwxHB.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVk1v4zYQ_TXUhYghkvo86BBbUGP00u4u0GNAUbTFghIFkbKb_PqClGTLjhqkTQ7dILCsmcd5wxm-ManW4thynoFwCzDOBW1__xVgDMLco4OpVZ-NNq9U1Uu2h6YXvIKmFhruvwHyCPwc-PNn5I__Up6a0VTxg2g5BPEWiigAeGcfEMQ5BIGvewZwYg2tGtqKHyDAIe2PM-zWigBOoVSMyuehbWnDq2daVT0EmPiWYCQsy0tSEOqzMKweKecYOyhpyaU1lGUM7b4nNHRA_w5Cbt33EfDkBmG-LIV1ELfo43-AbGHX80pDQPIx-hS87FcymoniLySyLyJx5qESp0Xl7LbnGi6wiDiwriVsh_OMF4mFX4Dvpk9s-isZxROjreT8bcnskF0tRs5wO6VzwcauJfZt9F33UJZ46XNn7OK6NnKkCd7woHUKtB59ce7u49vwoQsvWs17c6JyWJVKp4RW7cVis7Iv_jLP6COBHOMyzk2bem7-aVE09SzO75S-7OUfHDLawp53kjIOrXelLpEPz8LUs_u-PjNoA3_UvIWm5vAgWirh_hs8CylhyZlqODTqk8PHHM2_HT7rQwbgcJozrgOs6eAgzf3QwQsxCHRd9Gba3A2oFa3_FPK9iuSrRPuflfmOBP9vGvysBB-lOHH3y1Mb02l7SnEBcEGdfWOlsVG2pgXjABevABdPp_NfT9uNV2WkSklKPZ6hGCUxiiKSeHUWHwIax5yXlHCS0oAkfswJ81mQhnHJQ09k2MeBH6EEERSjYINYxTAJkxAx5EcBBYHPGyrkhd4TWg88S8MUpZ47Wnq6fzjtkkfVGdGIV2qEarW9jeAdwLgRWvPqYemcbip9Zhc-lMNRg8CXQht9JTPCSHe_-S6aTorDy674BYS5HUCF6s-0r747Je5UWwkb9If67Wlv59RICLWdN4xqrr2hl9ltZY_C1EO5YaoBuHDpj4-Hrld_cmYALtxuNcDFuOFThv8OAAD___e_dGg">