Skip to content

Commit 8c7faa6

Browse files
committed
mir-opt: Do not create storage marks for temporary locals
1 parent 145d226 commit 8c7faa6

7 files changed

+8
-69
lines changed

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,27 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
128128

129129
let mut patch = MirPatch::new(body);
130130

131-
let (second_discriminant_temp, second_operand) = if opt_data.need_hoist_discriminant {
131+
let second_operand = if opt_data.need_hoist_discriminant {
132132
// create temp to store second discriminant in, `_s` in example above
133133
let second_discriminant_temp =
134134
patch.new_temp(opt_data.child_ty, opt_data.child_source.span);
135135

136-
patch.add_statement(
137-
parent_end,
138-
StatementKind::StorageLive(second_discriminant_temp),
139-
);
140-
141136
// create assignment of discriminant
142137
patch.add_assign(
143138
parent_end,
144139
Place::from(second_discriminant_temp),
145140
Rvalue::Discriminant(opt_data.child_place),
146141
);
147-
(
148-
Some(second_discriminant_temp),
149-
Operand::Move(Place::from(second_discriminant_temp)),
150-
)
142+
Operand::Move(Place::from(second_discriminant_temp))
151143
} else {
152-
(None, Operand::Copy(opt_data.child_place))
144+
Operand::Copy(opt_data.child_place)
153145
};
154146

155147
// create temp to store inequality comparison between the two discriminants, `_t` in
156148
// example above
157149
let nequal = BinOp::Ne;
158150
let comp_res_type = nequal.ty(tcx, parent_ty, opt_data.child_ty);
159151
let comp_temp = patch.new_temp(comp_res_type, opt_data.child_source.span);
160-
patch.add_statement(parent_end, StatementKind::StorageLive(comp_temp));
161152

162153
// create inequality comparison
163154
let comp_rvalue =
@@ -193,42 +184,13 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
193184
let eq_bb = patch.new_block(eq_switch);
194185

195186
// Jump to it on the basis of the inequality comparison
196-
let mut true_case = opt_data.destination;
197-
let mut false_case = eq_bb;
198-
// Create an indirect BB to add `StorageDead` If the jump target is itself.
199-
for bb in [&mut false_case, &mut true_case].into_iter() {
200-
if *bb == parent {
201-
*bb = patch.new_block(BasicBlockData::new(
202-
Some(Terminator {
203-
kind: TerminatorKind::Goto { target: parent },
204-
source_info: bbs[parent].terminator().source_info,
205-
}),
206-
bbs[parent].is_cleanup,
207-
));
208-
}
209-
}
187+
let true_case = opt_data.destination;
188+
let false_case = eq_bb;
210189
patch.patch_terminator(
211190
parent,
212191
TerminatorKind::if_(Operand::Move(Place::from(comp_temp)), true_case, false_case),
213192
);
214193

215-
if let Some(second_discriminant_temp) = second_discriminant_temp {
216-
// generate StorageDead for the second_discriminant_temp not in use anymore
217-
patch.add_statement(
218-
parent_end,
219-
StatementKind::StorageDead(second_discriminant_temp),
220-
);
221-
}
222-
223-
// Generate a StorageDead for comp_temp in each of the targets, since we moved it into
224-
// the switch
225-
for bb in [false_case, true_case].into_iter() {
226-
patch.add_statement(
227-
Location { block: bb, statement_index: 0 },
228-
StatementKind::StorageDead(comp_temp),
229-
);
230-
}
231-
232194
patch.apply(body);
233195
}
234196

tests/mir-opt/early_otherwise_branch.opt3.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@
3232
StorageDead(_4);
3333
_9 = discriminant((_3.0: Option2<u32>));
3434
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
35-
+ StorageLive(_12);
3635
+ _12 = discriminant((_3.1: Option2<bool>));
37-
+ StorageLive(_13);
3836
+ _13 = Ne(copy _9, move _12);
39-
+ StorageDead(_12);
4037
+ switchInt(move _13) -> [0: bb7, otherwise: bb1];
4138
}
4239

4340
bb1: {
44-
+ StorageDead(_13);
4541
_0 = const 1_u32;
4642
- goto -> bb8;
4743
+ goto -> bb5;
@@ -100,7 +96,6 @@
10096
+ }
10197
+
10298
+ bb7: {
103-
+ StorageDead(_13);
10499
+ switchInt(copy _9) -> [0: bb4, 1: bb3, 2: bb2, otherwise: bb6];
105100
}
106101
}

tests/mir-opt/early_otherwise_branch.opt4.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@
3232
StorageDead(_4);
3333
_9 = discriminant((_3.0: Option2<u32>));
3434
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
35-
+ StorageLive(_12);
3635
+ _12 = discriminant((_3.1: Option2<u32>));
37-
+ StorageLive(_13);
3836
+ _13 = Ne(copy _9, move _12);
39-
+ StorageDead(_12);
4037
+ switchInt(move _13) -> [0: bb7, otherwise: bb1];
4138
}
4239

4340
bb1: {
44-
+ StorageDead(_13);
4541
_0 = const 1_u32;
4642
- goto -> bb8;
4743
+ goto -> bb5;
@@ -100,7 +96,6 @@
10096
+ }
10197
+
10298
+ bb7: {
103-
+ StorageDead(_13);
10499
+ switchInt(copy _9) -> [0: bb4, 1: bb3, 2: bb2, otherwise: bb6];
105100
}
106101
}

tests/mir-opt/early_otherwise_branch.opt5.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
StorageDead(_5);
2121
StorageDead(_4);
2222
- switchInt(copy (_3.0: u32)) -> [1: bb2, 2: bb3, 3: bb4, otherwise: bb1];
23-
+ StorageLive(_6);
2423
+ _6 = Ne(copy (_3.0: u32), copy (_3.1: u32));
2524
+ switchInt(move _6) -> [0: bb6, otherwise: bb1];
2625
}
2726

2827
bb1: {
29-
+ StorageDead(_6);
3028
_0 = const 0_u32;
3129
- goto -> bb8;
3230
+ goto -> bb5;
@@ -70,7 +68,6 @@
7068
- bb8: {
7169
- StorageDead(_3);
7270
- return;
73-
+ StorageDead(_6);
7471
+ switchInt(copy (_3.0: u32)) -> [1: bb4, 2: bb3, 3: bb2, otherwise: bb1];
7572
}
7673
}

tests/mir-opt/early_otherwise_branch.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ fn opt5_failed_type(x: u32, y: u64) -> u32 {
132132
#[custom_mir(dialect = "runtime")]
133133
fn target_self(val: i32) {
134134
// CHECK-LABEL: fn target_self(
135+
// CHECK: Ne(
136+
// CHECK-NEXT: switchInt
135137
mir! {
136138
{
137139
Goto(bb1)

tests/mir-opt/early_otherwise_branch.target_self.EarlyOtherwiseBranch.diff

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
bb1: {
1313
- switchInt(copy _1) -> [0: bb2, otherwise: bb1];
14-
+ StorageLive(_2);
1514
+ _2 = Ne(copy _1, copy _1);
16-
+ switchInt(move _2) -> [0: bb3, otherwise: bb4];
15+
+ switchInt(move _2) -> [0: bb3, otherwise: bb1];
1716
}
1817

1918
bb2: {
@@ -23,13 +22,7 @@
2322

2423
bb3: {
2524
- return;
26-
+ StorageDead(_2);
2725
+ switchInt(copy _1) -> [0: bb2, otherwise: bb1];
28-
+ }
29-
+
30-
+ bb4: {
31-
+ StorageDead(_2);
32-
+ goto -> bb1;
3326
}
3427
}
3528

tests/mir-opt/early_otherwise_branch_3_element_tuple.opt2.EarlyOtherwiseBranch.diff

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,12 @@
4242
StorageDead(_5);
4343
_14 = discriminant((_4.0: Option2<u32>));
4444
- switchInt(move _14) -> [0: bb2, 1: bb4, 2: bb6, otherwise: bb12];
45-
+ StorageLive(_18);
4645
+ _18 = discriminant((_4.1: Option2<u32>));
47-
+ StorageLive(_19);
4846
+ _19 = Ne(copy _14, move _18);
49-
+ StorageDead(_18);
5047
+ switchInt(move _19) -> [0: bb10, otherwise: bb1];
5148
}
5249

5350
bb1: {
54-
+ StorageDead(_19);
5551
_0 = const 1_u32;
5652
- goto -> bb11;
5753
+ goto -> bb8;
@@ -134,7 +130,6 @@
134130
+ }
135131
+
136132
+ bb10: {
137-
+ StorageDead(_19);
138133
+ switchInt(copy _14) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
139134
}
140135
}

0 commit comments

Comments
 (0)