Closed
Description
I expected the following:
fn f() -> usize {
1 + if true { 1 } else { 2 }
}
to const-fold into _0 = 2
.
Godbolt: https://godbolt.org/z/e89dYj
Using -Zdump_mir=all
shows that this may be a pass ordering issue.
Before SimplifyCfg-final
fn f() -> usize {
let mut _0: usize; // return place in scope 0 at test.rs:2:11: 2:16
let mut _1: usize; // in scope 0 at test.rs:3:9: 3:33
let mut _2: bool; // in scope 0 at test.rs:3:12: 3:16
bb0: {
StorageLive(_1); // scope 0 at test.rs:3:9: 3:33
StorageLive(_2); // scope 0 at test.rs:3:12: 3:16
_2 = const true; // scope 0 at test.rs:3:12: 3:16
goto -> bb1; // scope 0 at test.rs:3:9: 3:33
}
bb1: {
_1 = const 1_usize; // scope 0 at test.rs:3:19: 3:20
goto -> bb3; // scope 0 at test.rs:3:9: 3:33
}
bb2: {
_1 = const 2_usize; // scope 0 at test.rs:3:30: 3:31
goto -> bb3; // scope 0 at test.rs:3:9: 3:33
}
bb3: {
StorageDead(_2); // scope 0 at test.rs:3:32: 3:33
_0 = Add(const 1_usize, move _1); // scope 0 at test.rs:3:5: 3:33
StorageDead(_1); // scope 0 at test.rs:3:32: 3:33
return; // scope 0 at test.rs:4:2: 4:2
}
bb4 (cleanup): {
resume; // scope 0 at test.rs:2:1: 4:2
}
}
After SimplifyCfg-final
fn f() -> usize {
let mut _0: usize; // return place in scope 0 at test.rs:2:11: 2:16
let mut _1: usize; // in scope 0 at test.rs:3:9: 3:33
let mut _2: bool; // in scope 0 at test.rs:3:12: 3:16
bb0: {
StorageLive(_1); // scope 0 at test.rs:3:9: 3:33
StorageLive(_2); // scope 0 at test.rs:3:12: 3:16
_2 = const true; // scope 0 at test.rs:3:12: 3:16
_1 = const 1_usize; // scope 0 at test.rs:3:19: 3:20
StorageDead(_2); // scope 0 at test.rs:3:32: 3:33
_0 = Add(const 1_usize, move _1); // scope 0 at test.rs:3:5: 3:33
StorageDead(_1); // scope 0 at test.rs:3:32: 3:33
return; // scope 0 at test.rs:4:2: 4:2
}
}
So after SimplifyCfg-final
it is clear that _0 = Add(const 1_usize, move _1)
can be folded into _0 = 2
. However, this is not done as ConstProp is only running early in the pipeline.