<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/75752>75752</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Missed optimization: invert nested branches on comparisons
</td>
</tr>
<tr>
<th>Labels</th>
<td>
missed-optimization
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
Kmeakin
</td>
</tr>
</table>
<pre>
Reporting from https://github.com/rust-lang/rust/issues/117970
SimplifyCFG could invert nested branches to reduce code size and number of comparisons
# Rust example
[Compiler explorer](https://godbolt.org/z/zzYzvz8qq)
```rs
extern "Rust" {
fn foo() -> u32;
fn bar() -> u32;
fn baz() -> u32;
}
pub fn src(x: u32, y: u32) -> u32 {
unsafe {
match (x, y) {
(1, 10) => foo(),
(2, 10) => bar(),
(3, 10) => baz(),
_ => 0,
}
}
}
pub fn tgt(x: u32, y: u32) -> u32 {
unsafe {
match (y, x) {
(10, 1) => foo(),
(10, 2) => bar(),
(10, 3) => baz(),
_ => 0,
}
}
}
```
# C equivalent
[Compiler explorer](url)
```c
#include <stdint.h>
uint32_t foo();
uint32_t bar();
uint32_t baz();
uint32_t src(uint32_t x, uint32_t y) {
switch (x) {
case 1:
switch (y) {
case 10:
return foo();
default:
return 0;
}
case 2:
switch (y) {
case 10:
return bar();
default:
return 0;
}
case 3:
switch (y) {
case 10:
return baz();
default:
return 0;
}
default:
return 0;
}
}
uint32_t tgt(uint32_t x, uint32_t y) {
switch (y) {
case 10:
switch (x) {
case 1:
return foo();
case 2:
return bar();
case 3:
return baz();
default:
return 0;
}
default:
return 0;
}
}
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVs9zqzYQ_mvki8YeWNkGDhwS-7mHTi_pqac3AhZbrZCIfqQOf30HbOMfYOflNT2USTCg_b7VfrurWW6t2CrElCyeyWI94d7ttEl_rZD_JdQk08V7-oK1Nk6oLS2NrujOudoS9kRgQ2CzF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