Skip to content

Commit 118aae2

Browse files
committed
insert storageDead for not equal temp
1 parent 2530274 commit 118aae2

5 files changed

+37
-11
lines changed

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,32 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
4646

4747
for opt_to_apply in opts_to_apply {
4848
trace!("SUCCESS: found optimization possibility to apply: {:?}", &opt_to_apply);
49-
// create the patch using MirPatch
50-
let mut patch = MirPatch::new(body);
5149

52-
// create temp to store second discriminant in
53-
let discr_type = opt_to_apply.infos[0].second_switch_info.discr_ty;
54-
let discr_span = opt_to_apply.infos[0].second_switch_info.discr_source_info.span;
55-
let temp = patch.new_temp(discr_type, discr_span);
5650
let statements_before =
5751
body.basic_blocks()[opt_to_apply.basic_block_first_switch].statements.len();
5852
let end_of_block_location = Location {
5953
block: opt_to_apply.basic_block_first_switch,
6054
statement_index: statements_before,
6155
};
62-
patch.add_statement(end_of_block_location, StatementKind::StorageLive(temp));
56+
57+
let mut patch = MirPatch::new(body);
58+
59+
// create temp to store second discriminant in
60+
let discr_type = opt_to_apply.infos[0].second_switch_info.discr_ty;
61+
let discr_span = opt_to_apply.infos[0].second_switch_info.discr_source_info.span;
62+
let second_discriminant_temp = patch.new_temp(discr_type, discr_span);
63+
64+
patch.add_statement(
65+
end_of_block_location,
66+
StatementKind::StorageLive(second_discriminant_temp),
67+
);
6368

6469
// create assignment of discriminant
6570
let place_of_adt_to_get_discriminant_of =
6671
opt_to_apply.infos[0].second_switch_info.place_of_adt_discr_read;
6772
patch.add_assign(
6873
end_of_block_location,
69-
Place::from(temp),
74+
Place::from(second_discriminant_temp),
7075
Rvalue::Discriminant(place_of_adt_to_get_discriminant_of),
7176
);
7277

@@ -81,7 +86,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
8186
opt_to_apply.infos[0].first_switch_info.discr_used_in_switch;
8287
let not_equal_rvalue = Rvalue::BinaryOp(
8388
not_equal,
84-
Operand::Copy(Place::from(temp)),
89+
Operand::Copy(Place::from(second_discriminant_temp)),
8590
Operand::Copy(Place::from(first_descriminant_place)),
8691
);
8792
patch.add_statement(
@@ -126,8 +131,19 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
126131
),
127132
);
128133

129-
// generate StorageDead for the temp not in use anymore. We use the not_equal_temp in the switch, so we can't mark that dead
130-
patch.add_statement(end_of_block_location, StatementKind::StorageDead(temp));
134+
// generate StorageDead for the second_discriminant_temp not in use anymore
135+
patch.add_statement(
136+
end_of_block_location,
137+
StatementKind::StorageDead(second_discriminant_temp),
138+
);
139+
140+
// Generate a StorageDead for not_equal_temp in each of the targets, since we moved it into the switch
141+
for bb in [false_case, true_case].iter() {
142+
patch.add_statement(
143+
Location { block: *bb, statement_index: 0 },
144+
StatementKind::StorageDead(not_equal_temp),
145+
);
146+
}
131147

132148
patch.apply(body);
133149
}

src/test/mir-opt/early_otherwise_branch.opt1.EarlyOtherwiseBranch.diff

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
}
4141

4242
bb1: {
43+
+ StorageDead(_11); // scope 0 at $DIR/early_otherwise_branch.rs:6:14: 6:15
4344
_0 = const 1_u32; // scope 0 at $DIR/early_otherwise_branch.rs:6:14: 6:15
4445
goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
4546
}
@@ -70,6 +71,7 @@
7071
+ }
7172
+
7273
+ bb6: {
74+
+ StorageDead(_11); // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
7375
+ switchInt(_7) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch.rs:5:19: 5:26
7476
}
7577
}

src/test/mir-opt/early_otherwise_branch.opt2.EarlyOtherwiseBranch.diff

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
}
4747

4848
bb2: {
49+
+ StorageDead(_12); // scope 0 at $DIR/early_otherwise_branch.rs:15:14: 15:15
4950
_0 = const 1_u32; // scope 0 at $DIR/early_otherwise_branch.rs:15:14: 15:15
5051
goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
5152
}
@@ -81,6 +82,7 @@
8182
+ }
8283
+
8384
+ bb8: {
85+
+ StorageDead(_12); // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
8486
+ switchInt(_8) -> [0_isize: bb5, 1_isize: bb4, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch.rs:14:16: 14:20
8587
}
8688
}

src/test/mir-opt/early_otherwise_branch_3_element_tuple.opt1.EarlyOtherwiseBranch.diff

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
}
5252

5353
bb1: {
54+
+ StorageDead(_17); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:7:14: 7:15
55+
+ StorageDead(_15); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:7:14: 7:15
5456
_0 = const 1_u32; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:7:14: 7:15
5557
goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
5658
}
@@ -95,10 +97,12 @@
9597
+ }
9698
+
9799
+ bb7: {
100+
+ StorageDead(_15); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
98101
+ switchInt(_10) -> [1_isize: bb3, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:19: 6:26
99102
+ }
100103
+
101104
+ bb8: {
105+
+ StorageDead(_17); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
102106
+ switchInt(_9) -> [1_isize: bb4, otherwise: bb1]; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:28: 6:35
103107
}
104108
}

src/test/mir-opt/early_otherwise_branch_68867.try_sum.EarlyOtherwiseBranch.diff

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
}
8383

8484
bb2: {
85+
+ StorageDead(_35); // scope 0 at $DIR/early_otherwise_branch_68867.rs:24:25: 24:27
8586
StorageLive(_33); // scope 0 at $DIR/early_otherwise_branch_68867.rs:24:25: 24:27
8687
((_0 as Err).0: ()) = const (); // scope 0 at $DIR/early_otherwise_branch_68867.rs:24:21: 24:28
8788
discriminant(_0) = 1; // scope 0 at $DIR/early_otherwise_branch_68867.rs:24:21: 24:28
@@ -207,6 +208,7 @@
207208
+ }
208209
+
209210
+ bb13: {
211+
+ StorageDead(_35); // scope 0 at $DIR/early_otherwise_branch_68867.rs:20:21: 20:30
210212
+ switchInt(_11) -> [0_isize: bb6, 1_isize: bb7, 2_isize: bb8, 3_isize: bb9, otherwise: bb2]; // scope 0 at $DIR/early_otherwise_branch_68867.rs:20:21: 20:30
211213
}
212214
}

0 commit comments

Comments
 (0)