Skip to content

Commit 9218a47

Browse files
committed
GVN unnit analysis shouldn't check unreachable blocks
1 parent 80fce82 commit 9218a47

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
154154
maybe_uninit,
155155
};
156156

157-
storage_checker.visit_body(body);
157+
for (bb, data) in traversal::reachable(body) {
158+
storage_checker.visit_basic_block_data(bb, data);
159+
}
158160

159161
storage_checker.storage_to_remove
160162
} else {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
- // MIR for `f` before GVN
2+
+ // MIR for `f` after GVN
3+
4+
fn f(_1: u32) -> () {
5+
let mut _0: ();
6+
let mut _2: S;
7+
let mut _3: S;
8+
let mut _4: S;
9+
10+
bb0: {
11+
StorageLive(_2);
12+
_2 = S(copy _1, const 2_u32);
13+
StorageLive(_3);
14+
- _3 = S(copy _1, const 2_u32);
15+
+ _3 = copy _2;
16+
StorageDead(_3);
17+
StorageDead(_2);
18+
return;
19+
}
20+
21+
bb1: {
22+
StorageLive(_2);
23+
_4 = copy _2;
24+
StorageDead(_2);
25+
return;
26+
}
27+
}
28+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//! Check that we do not remove the storage statements if a reused local
2+
//! is uninitialized in an unreachable block.
3+
//@ test-mir-pass: GVN
4+
// EMIT_MIR gvn_storage_unreachable.f.GVN.diff
5+
6+
#![feature(custom_mir, core_intrinsics)]
7+
8+
use std::intrinsics::mir::*;
9+
10+
struct S(u32, u32);
11+
12+
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
13+
pub fn f(_1: u32) {
14+
// CHECK-LABEL: fn f(
15+
mir! {
16+
let _2: S;
17+
let _3: S;
18+
let _4: S;
19+
{
20+
// CHECK: StorageLive(_2);
21+
// CHECK: StorageLive(_3);
22+
// CHECK: _3 = copy _2;
23+
// CHECK: StorageDead(_3);
24+
// CHECK: StorageDead(_2);
25+
StorageLive(_2);
26+
_2 = S(_1, 2);
27+
StorageLive(_3);
28+
_3 = S(_1, 2);
29+
StorageDead(_3);
30+
StorageDead(_2);
31+
Return()
32+
}
33+
bb1 = {
34+
StorageLive(_2);
35+
// CHECK: _4 = copy _2;
36+
_4 = _2;
37+
StorageDead(_2);
38+
Return()
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)