Skip to content

Optimize if conditions with integer equality to matches on the integer #75144

Closed
@oli-obk

Description

@oli-obk

If we generate the MIR for

fn black_box<T>(t: T) -> T { t }

fn main() {
    let x = black_box(42);
    
    let y = if x == 43 {
        "foo"
    } else {
        "bar"
    };
}

we get something like

        _3 = Eq(move _4, const 43i32);
        StorageDead(_4);
        switchInt(_3) -> [false: bb2, otherwise: bb3];

which we could also write as

        switchInt(_4) -> [43i32: bb3, otherwise: bb2];

(plus move the StorageDead to the beginning of bb2 and bb3). The same goes for != (Ne).

cc @rust-lang/wg-mir-opt

Metadata

Metadata

Assignees

Labels

A-mir-optArea: MIR optimizationsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions