Skip to content

Missed optimization: invert nested switches #75752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Kmeakin opened this issue Dec 17, 2023 · 1 comment
Open

Missed optimization: invert nested switches #75752

Kmeakin opened this issue Dec 17, 2023 · 1 comment

Comments

@Kmeakin
Copy link
Contributor

Kmeakin commented Dec 17, 2023

Reporting from rust-lang/rust#117970

SimplifyCFG could invert nested switches to reduce code size and number of comparisons

Rust example

Compiler explorer

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

#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;
    }
}
@Kmeakin Kmeakin changed the title Missed optimization: invert nested branches on comparisons Missed optimization: invert nested switches Dec 17, 2023
@UsmanNadeem
Copy link
Contributor

Hi, we have a downstream pass that does similar optimizations for switch statements, it is geared towards switch based state machines used in embedded products.

We are planning to upstream it in the near future. Please let me know if anyone is planning to work on this ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants