Skip to content

Commit cf8c665

Browse files
committed
Add mir-opt tests
1 parent 18a5514 commit cf8c665

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

compiler/rustc_mir/src/transform/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
413413
&nrvo::RenameReturnPlace,
414414
&simplify::SimplifyLocals,
415415
&multiple_return_terminators::MultipleReturnTerminators,
416+
&match_identity::MatchIdentitySimplification,
416417
];
417418

418419
// Optimizations to run even if mir optimizations have been disabled.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
- // MIR for `flip_flop` before MatchBranchSimplification
2+
+ // MIR for `flip_flop` after MatchBranchSimplification
3+
4+
fn flip_flop(_1: std::result::Result<i32, u32>) -> std::result::Result<u32, i32> {
5+
debug a => _1; // in scope 0 at $DIR/match_identity.rs:16:18: 16:19
6+
let mut _0: std::result::Result<u32, i32>; // return place in scope 0 at $DIR/match_identity.rs:16:38: 16:50
7+
let mut _2: isize; // in scope 0 at $DIR/match_identity.rs:18:9: 18:14
8+
let _3: i32; // in scope 0 at $DIR/match_identity.rs:18:12: 18:13
9+
let mut _4: i32; // in scope 0 at $DIR/match_identity.rs:18:22: 18:23
10+
let _5: u32; // in scope 0 at $DIR/match_identity.rs:19:13: 19:14
11+
let mut _6: u32; // in scope 0 at $DIR/match_identity.rs:19:22: 19:23
12+
scope 1 {
13+
debug x => _3; // in scope 1 at $DIR/match_identity.rs:18:12: 18:13
14+
}
15+
scope 2 {
16+
debug y => _5; // in scope 2 at $DIR/match_identity.rs:19:13: 19:14
17+
}
18+
19+
bb0: {
20+
_2 = discriminant(_1); // scope 0 at $DIR/match_identity.rs:18:9: 18:14
21+
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/match_identity.rs:18:9: 18:14
22+
}
23+
24+
bb1: {
25+
StorageLive(_5); // scope 0 at $DIR/match_identity.rs:19:13: 19:14
26+
_5 = ((_1 as Err).0: u32); // scope 0 at $DIR/match_identity.rs:19:13: 19:14
27+
StorageLive(_6); // scope 2 at $DIR/match_identity.rs:19:22: 19:23
28+
_6 = _5; // scope 2 at $DIR/match_identity.rs:19:22: 19:23
29+
((_0 as Ok).0: u32) = move _6; // scope 2 at $DIR/match_identity.rs:19:19: 19:24
30+
discriminant(_0) = 0; // scope 2 at $DIR/match_identity.rs:19:19: 19:24
31+
StorageDead(_6); // scope 2 at $DIR/match_identity.rs:19:23: 19:24
32+
StorageDead(_5); // scope 0 at $DIR/match_identity.rs:19:23: 19:24
33+
goto -> bb3; // scope 0 at $DIR/match_identity.rs:17:5: 20:6
34+
}
35+
36+
bb2: {
37+
StorageLive(_3); // scope 0 at $DIR/match_identity.rs:18:12: 18:13
38+
_3 = ((_1 as Ok).0: i32); // scope 0 at $DIR/match_identity.rs:18:12: 18:13
39+
StorageLive(_4); // scope 1 at $DIR/match_identity.rs:18:22: 18:23
40+
_4 = _3; // scope 1 at $DIR/match_identity.rs:18:22: 18:23
41+
((_0 as Err).0: i32) = move _4; // scope 1 at $DIR/match_identity.rs:18:18: 18:24
42+
discriminant(_0) = 1; // scope 1 at $DIR/match_identity.rs:18:18: 18:24
43+
StorageDead(_4); // scope 1 at $DIR/match_identity.rs:18:23: 18:24
44+
StorageDead(_3); // scope 0 at $DIR/match_identity.rs:18:23: 18:24
45+
goto -> bb3; // scope 0 at $DIR/match_identity.rs:17:5: 20:6
46+
}
47+
48+
bb3: {
49+
return; // scope 0 at $DIR/match_identity.rs:21:2: 21:2
50+
}
51+
}
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
- // MIR for `id_result` before MatchIdentitySimplification
2+
+ // MIR for `id_result` after MatchIdentitySimplification
3+
4+
fn id_result(_1: std::result::Result<i32, u32>) -> std::result::Result<i32, u32> {
5+
debug a => _1; // in scope 0 at $DIR/match_identity.rs:8:18: 8:19
6+
let mut _0: std::result::Result<i32, u32>; // return place in scope 0 at $DIR/match_identity.rs:8:38: 8:50
7+
let mut _2: isize; // in scope 0 at $DIR/match_identity.rs:10:9: 10:14
8+
scope 1 {
9+
debug x => ((_0 as Ok).0: i32); // in scope 1 at $DIR/match_identity.rs:10:12: 10:13
10+
}
11+
scope 2 {
12+
debug y => ((_0 as Err).0: u32); // in scope 2 at $DIR/match_identity.rs:11:13: 11:14
13+
}
14+
15+
bb0: {
16+
_2 = discriminant(_1); // scope 0 at $DIR/match_identity.rs:10:9: 10:14
17+
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/match_identity.rs:10:9: 10:14
18+
}
19+
20+
bb1: {
21+
((_0 as Err).0: u32) = ((_1 as Err).0: u32); // scope 0 at $DIR/match_identity.rs:11:13: 11:14
22+
discriminant(_0) = 1; // scope 2 at $DIR/match_identity.rs:11:19: 11:25
23+
return; // scope 0 at $DIR/match_identity.rs:9:5: 12:6
24+
}
25+
26+
bb2: {
27+
((_0 as Ok).0: i32) = ((_1 as Ok).0: i32); // scope 0 at $DIR/match_identity.rs:10:12: 10:13
28+
discriminant(_0) = 0; // scope 1 at $DIR/match_identity.rs:10:18: 10:23
29+
return; // scope 0 at $DIR/match_identity.rs:9:5: 12:6
30+
}
31+
}
32+

src/test/mir-opt/match_identity.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
// EMIT_MIR match_identity.id_result.match_identity.diff
2-
pub fn id_result(a: Result<u64, i64>) -> Result<u64, i64> {
1+
#![crate_type = "lib"]
2+
3+
type T = i32;
4+
type E = u32;
5+
6+
// EMIT_MIR_FOR_EACH_BIT_WIDTH
7+
// EMIT_MIR match_identity.id_result.MatchIdentitySimplification.diff
8+
pub fn id_result(a: Result<T, E>) -> Result<T, E> {
39
match a {
410
Ok(x) => Ok(x),
511
Err(y) => Err(y),
612
}
713
}
814

9-
// EMIT_MIR match_identity.id_result.match_identity.diff
10-
pub fn flip_flop(a: Result<u64, i64>) -> Result<i64, u64> {
15+
// EMIT_MIR match_identity.flip_flop.MatchBranchSimplification.diff
16+
pub fn flip_flop(a: Result<T, E>) -> Result<E, T> {
1117
match a {
1218
Ok(x) => Err(x),
1319
Err(y) => Ok(y),

0 commit comments

Comments
 (0)