llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.31k stars 12.11k forks source link

Failure to infer that `ctpop(y) == 1` implies `y != 0` #115751

Open Kmeakin opened 2 weeks ago

Kmeakin commented 2 weeks ago

If y.is_power_of_two() is true, then y cannot be 0, so the panic branch should be eliminated

https://godbolt.org/z/q9jhsnqs7

pub fn div(x: u32, y: u32) -> u32 {
    if y.is_power_of_two() {
        x / y
    } else {
        0
    }
}

pub fn rem(x: u32, y: u32) -> u32 {
    if y.is_power_of_two() {
        x % y
    } else {
        0
    }
}
nikic commented 2 weeks ago

Should probably be addressed in LVI/CVP.