Skip to content

Commit 442e064

Browse files
committed
Reduce the number of drop-flag assignments in unwind paths
1 parent ebe5971 commit 442e064

File tree

3 files changed

+18
-44
lines changed

3 files changed

+18
-44
lines changed

src/librustc_mir/dataflow/move_paths/builder.rs

Lines changed: 1 addition & 4 deletions
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

Lines changed: 11 additions & 31 deletions
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());
@@ -842,26 +830,18 @@ where
842830
}
843831

844832
/// Returns a basic block that drop a place using the context
845-
/// and path in `c`. If `mode` is something, also clear `c`
846-
/// according to it.
833+
/// and path in `c`.
847834
///
848835
/// if FLAG(self.path)
849-
/// if let Some(mode) = mode: FLAG(self.path)[mode] = false
850836
/// drop(self.place)
851837
fn complete_drop(
852838
&mut self,
853-
drop_mode: Option<DropFlagMode>,
854839
succ: BasicBlock,
855840
unwind: Unwind,
856841
) -> BasicBlock {
857-
debug!("complete_drop({:?},{:?})", self, drop_mode);
842+
debug!("complete_drop(succ={:?}, unwind={:?})", succ, unwind);
858843

859844
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-
};
865845

866846
self.drop_flag_test_block(drop_block, succ, unwind)
867847
}
@@ -873,6 +853,11 @@ where
873853
{
874854
debug!("drop_flag_reset_block({:?},{:?})", self, mode);
875855

856+
if unwind.is_cleanup() {
857+
// The drop flag isn't read again on the unwind path, so don't
858+
// bother setting it.
859+
return succ;
860+
}
876861
let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
877862
let block_start = Location { block: block, statement_index: 0 };
878863
self.elaborator.clear_drop_flag(block_start, self.path, mode);
@@ -976,11 +961,6 @@ where
976961
self.elaborator.patch().new_temp(ty, self.source_info.span)
977962
}
978963

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-
984964
fn constant_usize(&self, val: u16) -> Operand<'tcx> {
985965
Operand::Constant(box Constant {
986966
span: self.source_info.span,

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ fn main() {
4040
// END rustc.E-V-{{constant}}.mir_map.0.mir
4141

4242
// START rustc.ptr-real_drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir
43-
// bb0: {
44-
// goto -> bb7;
43+
// bb0: {
44+
// goto -> bb6;
4545
// }
4646
// bb1: {
4747
// return;
@@ -53,17 +53,14 @@ fn main() {
5353
// goto -> bb1;
5454
// }
5555
// bb4 (cleanup): {
56-
// goto -> bb2;
56+
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> bb2;
5757
// }
58-
// bb5 (cleanup): {
59-
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> bb4;
58+
// bb5: {
59+
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> [return: bb3, unwind: bb2];
6060
// }
6161
// bb6: {
62-
// drop(((*_1).0: alloc::raw_vec::RawVec<i32>)) -> [return: bb3, unwind: bb4];
63-
// }
64-
// bb7: {
6562
// _2 = &mut (*_1);
66-
// _3 = const <std::vec::Vec<i32> as std::ops::Drop>::drop(move _2) -> [return: bb6, unwind: bb5];
63+
// _3 = const <std::vec::Vec<i32> as std::ops::Drop>::drop(move _2) -> [return: bb5, unwind: bb4];
6764
// }
6865
// END rustc.ptr-real_drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir
6966

0 commit comments

Comments
 (0)