<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63756>63756</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Select not sunk into branch on same condition
</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://llvm.godbolt.org/z/s61x8M7Tf
```llvm
declare void @use(i32)
declare void @dummy()
define void @test(i1 %c, i32 %a, i32 %b) {
%s = select i1 %c, i32 %a, i32 %b
br i1 %c, label %if, label %else
if:
call void @dummy()
call void @use(i32 %s)
ret void
else:
call void @use(i32 %s)
call void @dummy()
ret void
}
```
We have a branch on the same condition as the select, so we should sink the select arms into the branches. It's pointless to compute the select value in the common code path:
```llvm
declare void @use(i32)
declare void @dummy()
define void @test(i1 %c, i32 %a, i32 %b) {
br i1 %c, label %if, label %else
if:
call void @dummy()
call void @use(i32 %a)
ret void
else:
call void @use(i32 %b)
call void @dummy()
ret void
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVE1v4zgM_TXyhWig0N8HH9oGAfawp11gz7JEx9rKUmDKmWl__cB2pnWDKXopMAMEiEQ-fuj5kYrZnjxRI_IHkR8SNcU-jI23T1YnbTDPTR_jmUV6L_Ao8OjcZdidgmmDi7swngQeXwQeudh_r_4u_-2EPAh5Lwq5_mb4ajKknRoJLsEaEJmcmARWNkWB9a8RZhqGZ4HVK-AnrLP-DRWJ45xoDwJzLfARbIrzWW3OrcAaRPmwZoDZxCDSAzA50hE-i76GteMW6VRLbr7Z7t2VHNO2YdvN7F1TaOXcxy-88b9ytDS8QY0UF9C2zFL2g0IfJvqsnZtC5eHm-24b-I-gVxcCBe2ovO4heIg9AauBQAdvbLTBg-LVujA_E8cBvhFwHyZngK1_2vhBjQOD9TEsxjUx8Q7-igJLhnOwPjpihhhAh-E8RdqGX5SbCOzaiA7DEDzoYAjOKvavbP3pcv2tulNfpbv2C3WXmCY1dVqrhJp9UVUVFnkuk74pjOl0K7PM5Lk0nepqSUh5Idtsr2vCxDYoMZWlrPYZ1nu5q7tStSbLZFZoVWSVyCQNyrrdsurCeEos80RNkZZ5kSxs87ItERe5pPfhHO1gX9QsbxaIAh8F4mCZydxtnbMvPyRjMwfetdOJRSad5chvxaKNjpp_Vvn6EIEn_7ROwNtYvR-pZBrdzZo-2dhP7U6H4bqzr3935zH8v8zdcXkVCzwuD_sRAAD__yU7wA8">