<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64031>64031</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[SimplifyCFG] Hoist common instructions on the switch instruction.
</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>
https://alive2.llvm.org/ce/z/wGyVbz
```llvm
define i1 @src(i64 %a, i64 %b, i64 %c, i64 %d) {
start:
%_5 = icmp eq i64 %a, %c
br i1 %_5, label %bb2, label %bb3
bb2: ; preds = %start
switch i64 %a, label %bb5 [
i64 0, label %bb4
i64 1, label %bb6
i64 2, label %bb7
]
bb3: ; preds = %bb4, %bb6, %bb7, %start
%_0.0 = phi i1 [ false, %start ], [ %2, %bb7 ], [ %1, %bb6 ], [ %0, %bb4 ]
ret i1 %_0.0
bb5: ; preds = %bb2
unreachable
bb4: ; preds = %bb2
%0 = icmp eq i64 %b, %d
br label %bb3
bb6: ; preds = %bb2
%1 = icmp eq i64 %b, %d
br label %bb3
bb7: ; preds = %bb2
%2 = icmp eq i64 %b, %d
br label %bb3
}
```
We could hoist common instructions, like this:
```llvm
define i1 @tgt(i64 %a, i64 %b, i64 %c, i64 %d) {
start:
%_5 = icmp eq i64 %a, %c
br i1 %_5, label %bb2, label %bb3
bb2: ; preds = %start
%0 = icmp eq i64 %b, %d
switch i64 %a, label %bb5 [
i64 0, label %bb4
i64 1, label %bb6
i64 2, label %bb7
]
bb3: ; preds = %bb4, %bb6, %bb7, %start
%_0.0 = phi i1 [ false, %start ], [ %0, %bb7 ], [ %0, %bb6 ], [ %0, %bb4 ]
ret i1 %_0.0
bb5: ; preds = %bb2
unreachable
bb4: ; preds = %bb2
br label %bb3
bb6: ; preds = %bb2
br label %bb3
bb7: ; preds = %bb2
br label %bb3
}
```
Final optimization is:
```llvm
define i1 @tgt(i64 %a, i64 %b, i64 %c, i64 %d) {
%_5 = icmp eq i64 %a, %c
%0 = icmp eq i64 %b, %d
%_0.0 = select i1 %_5, i1 %0, i1 false
ret i1 %_0.0
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVs1u2zgQfhrqQsSghqIUH3SwYygL7GmxQHssKIq22FI_FakEydMXpBRbUuI6QYICLRoE9lDz93Fm9Hm4MepQS5kitkUAO8Xr__5FAIjtAt7bsunS4VmQN8VDWlrbGkQ3CDIEGdfqTsJK67tq1XQHBJmQCLJHBNn97cOn_BGRHSKb8TMmw78zHx4Vcq9qiVWIUURMJxBcqzjCCBhHcINHOZ_IYiIXCNYYJdshlrG8sw6ZP2Fn8IVhRHdYiarF8juehvahRsu88wicvVNpnkvt8-awONPpfZyabjCiW9x2sjA-FwI2ABljm3tlRTlLPYnHsKv6aIq9FVmYRHN1uFDHc_UScHKsBtvNsVOH_U1_zy7qwA2ldDiepGSUZmVwxSUr4j3bUvl6sy3ec23k1N7jdGe2dc_gFHWhCU-ZFxpy1ESnW2PcSfvUZbIi81qwF_vo-js693UnuSh5ruXcM7rk6QC9NIT5iLKYDOH5QYtfkSZ8f5rkI2ZiignegSnZLWhjOH6WWDS9LnDZKGOxaKqqqbGqje16YVVTG_8KqG8S21KZIyFcYh97sH8e-7x6-v7S1HtoipylKfKb0NRl9vkwWrjMQG_3PsMVmaq5xk1rVaUeuSMH_OsY4S0c8Or3dDqgRmop7Iw8BpmM4jC2PxusZ3ULipQWa7rmgUzDeE0IJCRKgjKVcchoFBY0JGuQayYYE-vrcB_nLN6HjAYqBQKUJABhyBJIVrnkeymYoIW8FgUvUERkxZU-bouBMqaXaRwRGga-qWZcQn1D6GbaOONWUrhBAJUyRhZXU-W4rnapc7zK-4NBEdHKWHNKZpXVfsn9X1WtVvuHm-wWsR3-59zPCG5qbEt5pMaTahX0nV5swgdlyz5fiaZCkHn8w9dV2zVfpbAIMn9dgyDzN_4RAAD__6il6To">