Skip to content

Commit 4229e2b

Browse files
committed
Reduce the number of drop-flag assignments in unwind paths
1 parent 37e0352 commit 4229e2b

File tree

3 files changed

+17
-41
lines changed

3 files changed

+17
-41
lines changed

src/librustc_mir/dataflow/move_paths/builder.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -348,17 +348,14 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
348348
fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
349349
match term.kind {
350350
TerminatorKind::Goto { target: _ }
351+
| TerminatorKind::Return
351352
| TerminatorKind::Resume
352353
| TerminatorKind::Abort
353354
| TerminatorKind::GeneratorDrop
354355
| TerminatorKind::FalseEdges { .. }
355356
| TerminatorKind::FalseUnwind { .. }
356357
| TerminatorKind::Unreachable => {}
357358

358-
TerminatorKind::Return => {
359-
self.gather_move(&Place::return_place());
360-
}
361-
362359
TerminatorKind::Assert { ref cond, .. } => {
363360
self.gather_operand(cond);
364361
}

src/librustc_mir/util/elaborate_drops.rs

+10-28
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,14 @@ where
163163
});
164164
}
165165
DropStyle::Static => {
166-
let loc = self.terminator_loc(bb);
167-
self.elaborator.clear_drop_flag(loc, self.path, DropFlagMode::Deep);
168166
self.elaborator.patch().patch_terminator(bb, TerminatorKind::Drop {
169167
location: self.place.clone(),
170168
target: self.succ,
171169
unwind: self.unwind.into_option(),
172170
});
173171
}
174172
DropStyle::Conditional => {
175-
let unwind = self.unwind; // FIXME(#43234)
176-
let succ = self.succ;
177-
let drop_bb = self.complete_drop(Some(DropFlagMode::Deep), succ, unwind);
173+
let drop_bb = self.complete_drop(self.succ, self.unwind);
178174
self.elaborator.patch().patch_terminator(bb, TerminatorKind::Goto {
179175
target: drop_bb
180176
});
@@ -236,7 +232,7 @@ where
236232
// Using `self.path` here to condition the drop on
237233
// our own drop flag.
238234
path: self.path
239-
}.complete_drop(None, succ, unwind)
235+
}.complete_drop(succ, unwind)
240236
}
241237
}
242238

@@ -265,13 +261,7 @@ where
265261
// Clear the "master" drop flag at the end. This is needed
266262
// because the "master" drop protects the ADT's discriminant,
267263
// which is invalidated after the ADT is dropped.
268-
let (succ, unwind) = (self.succ, self.unwind); // FIXME(#43234)
269-
(
270-
self.drop_flag_reset_block(DropFlagMode::Shallow, succ, unwind),
271-
unwind.map(|unwind| {
272-
self.drop_flag_reset_block(DropFlagMode::Shallow, unwind, Unwind::InCleanup)
273-
})
274-
)
264+
(self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind), self.unwind)
275265
}
276266

277267
/// Creates a full drop ladder, consisting of 2 connected half-drop-ladders
@@ -827,9 +817,7 @@ where
827817
}
828818
}
829819
ty::Dynamic(..) => {
830-
let unwind = self.unwind; // FIXME(#43234)
831-
let succ = self.succ;
832-
self.complete_drop(Some(DropFlagMode::Deep), succ, unwind)
820+
self.complete_drop(self.succ, self.unwind)
833821
}
834822
ty::Array(ety, size) => {
835823
let size = size.try_eval_usize(self.tcx(), self.elaborator.param_env());
@@ -850,18 +838,12 @@ where
850838
/// drop(self.place)
851839
fn complete_drop(
852840
&mut self,
853-
drop_mode: Option<DropFlagMode>,
854841
succ: BasicBlock,
855842
unwind: Unwind,
856843
) -> BasicBlock {
857-
debug!("complete_drop({:?},{:?})", self, drop_mode);
844+
debug!("complete_drop(succ={:?}, unwind={:?})", succ, unwind);
858845

859846
let drop_block = self.drop_block(succ, unwind);
860-
let drop_block = if let Some(mode) = drop_mode {
861-
self.drop_flag_reset_block(mode, drop_block, unwind)
862-
} else {
863-
drop_block
864-
};
865847

866848
self.drop_flag_test_block(drop_block, succ, unwind)
867849
}
@@ -873,6 +855,11 @@ where
873855
{
874856
debug!("drop_flag_reset_block({:?},{:?})", self, mode);
875857

858+
if unwind.is_cleanup() {
859+
// The drop flag isn't read again on the unwind path, so don't
860+
// bother setting it.
861+
return succ;
862+
}
876863
let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
877864
let block_start = Location { block: block, statement_index: 0 };
878865
self.elaborator.clear_drop_flag(block_start, self.path, mode);
@@ -976,11 +963,6 @@ where
976963
self.elaborator.patch().new_temp(ty, self.source_info.span)
977964
}
978965

979-
fn terminator_loc(&mut self, bb: BasicBlock) -> Location {
980-
let body = self.elaborator.body();
981-
self.elaborator.patch().terminator_loc(body, bb)
982-
}
983-
984966
fn constant_usize(&self, val: u16) -> Operand<'tcx> {
985967
Operand::Constant(box Constant {
986968
span: self.source_info.span,

src/test/mir-opt/unusual-item-types.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ fn main() {
4646
// END rustc.E-V-{{constant}}.mir_map.0.mir
4747

4848
// START rustc.ptr-real_drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir
49-
// bb0: {
50-
// goto -> bb7;
49+
// bb0: {
50+
// goto -> bb6;
5151
// }
5252
// bb1: {
5353
// return;
@@ -59,17 +59,14 @@ fn main() {
5959
// goto -> bb1;
6060
// }
6161
// bb4 (cleanup): {
62-
// goto -> bb2;
62+
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> bb2;
6363
// }
64-
// bb5 (cleanup): {
65-
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> bb4;
64+
// bb5: {
65+
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> [return: bb3, unwind: bb2];
6666
// }
6767
// bb6: {
68-
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> [return: bb3, unwind: bb4];
69-
// }
70-
// bb7: {
7168
// _2 = &mut (*_1);
72-
// _3 = const <std::vec::Vec<i32> as std::ops::Drop>::drop(move _2) -> [return: bb6, unwind: bb5];
69+
// _3 = const <std::vec::Vec<i32> as std::ops::Drop>::drop(move _2) -> [return: bb5, unwind: bb4];
7370
// }
7471
// END rustc.ptr-real_drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir
7572

0 commit comments

Comments
 (0)