Skip to content

Commit ce31497

Browse files
committed
Reduce the number of drop-flag assignments in unwind paths
1 parent b4eeee6 commit ce31497

File tree

3 files changed

+19
-51
lines changed

3 files changed

+19
-51
lines changed

src/librustc_mir/dataflow/move_paths/builder.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,14 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
361361
fn gather_terminator(&mut self, term: &Terminator<'tcx>) {
362362
match term.kind {
363363
TerminatorKind::Goto { target: _ }
364+
| TerminatorKind::Return
364365
| TerminatorKind::Resume
365366
| TerminatorKind::Abort
366367
| TerminatorKind::GeneratorDrop
367368
| TerminatorKind::FalseEdges { .. }
368369
| TerminatorKind::FalseUnwind { .. }
369370
| TerminatorKind::Unreachable => {}
370371

371-
TerminatorKind::Return => {
372-
self.gather_move(&Place::return_place());
373-
}
374-
375372
TerminatorKind::Assert { ref cond, .. } => {
376373
self.gather_operand(cond);
377374
}

src/librustc_mir/util/elaborate_drops.rs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ where
163163
.patch_terminator(bb, TerminatorKind::Goto { target: self.succ });
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(
169167
bb,
170168
TerminatorKind::Drop {
@@ -175,9 +173,7 @@ where
175173
);
176174
}
177175
DropStyle::Conditional => {
178-
let unwind = self.unwind; // FIXME(#43234)
179-
let succ = self.succ;
180-
let drop_bb = self.complete_drop(Some(DropFlagMode::Deep), succ, unwind);
176+
let drop_bb = self.complete_drop(self.succ, self.unwind);
181177
self.elaborator
182178
.patch()
183179
.patch_terminator(bb, TerminatorKind::Goto { target: drop_bb });
@@ -249,7 +245,7 @@ where
249245
// our own drop flag.
250246
path: self.path,
251247
}
252-
.complete_drop(None, succ, unwind)
248+
.complete_drop(succ, unwind)
253249
}
254250
}
255251

@@ -280,13 +276,7 @@ where
280276
// Clear the "master" drop flag at the end. This is needed
281277
// because the "master" drop protects the ADT's discriminant,
282278
// which is invalidated after the ADT is dropped.
283-
let (succ, unwind) = (self.succ, self.unwind); // FIXME(#43234)
284-
(
285-
self.drop_flag_reset_block(DropFlagMode::Shallow, succ, unwind),
286-
unwind.map(|unwind| {
287-
self.drop_flag_reset_block(DropFlagMode::Shallow, unwind, Unwind::InCleanup)
288-
}),
289-
)
279+
(self.drop_flag_reset_block(DropFlagMode::Shallow, self.succ, self.unwind), self.unwind)
290280
}
291281

292282
/// Creates a full drop ladder, consisting of 2 connected half-drop-ladders
@@ -813,11 +803,7 @@ where
813803
self.open_drop_for_adt(def, substs)
814804
}
815805
}
816-
ty::Dynamic(..) => {
817-
let unwind = self.unwind; // FIXME(#43234)
818-
let succ = self.succ;
819-
self.complete_drop(Some(DropFlagMode::Deep), succ, unwind)
820-
}
806+
ty::Dynamic(..) => self.complete_drop(self.succ, self.unwind),
821807
ty::Array(ety, size) => {
822808
let size = size.try_eval_usize(self.tcx(), self.elaborator.param_env());
823809
self.open_drop_for_array(ety, size)
@@ -829,26 +815,14 @@ where
829815
}
830816

831817
/// Returns a basic block that drop a place using the context
832-
/// and path in `c`. If `mode` is something, also clear `c`
833-
/// according to it.
818+
/// and path in `c`.
834819
///
835820
/// if FLAG(self.path)
836-
/// if let Some(mode) = mode: FLAG(self.path)[mode] = false
837821
/// drop(self.place)
838-
fn complete_drop(
839-
&mut self,
840-
drop_mode: Option<DropFlagMode>,
841-
succ: BasicBlock,
842-
unwind: Unwind,
843-
) -> BasicBlock {
844-
debug!("complete_drop({:?},{:?})", self, drop_mode);
822+
fn complete_drop(&mut self, succ: BasicBlock, unwind: Unwind) -> BasicBlock {
823+
debug!("complete_drop(succ={:?}, unwind={:?})", succ, unwind);
845824

846825
let drop_block = self.drop_block(succ, unwind);
847-
let drop_block = if let Some(mode) = drop_mode {
848-
self.drop_flag_reset_block(mode, drop_block, unwind)
849-
} else {
850-
drop_block
851-
};
852826

853827
self.drop_flag_test_block(drop_block, succ, unwind)
854828
}
@@ -861,6 +835,11 @@ where
861835
) -> BasicBlock {
862836
debug!("drop_flag_reset_block({:?},{:?})", self, mode);
863837

838+
if unwind.is_cleanup() {
839+
// The drop flag isn't read again on the unwind path, so don't
840+
// bother setting it.
841+
return succ;
842+
}
864843
let block = self.new_block(unwind, TerminatorKind::Goto { target: succ });
865844
let block_start = Location { block: block, statement_index: 0 };
866845
self.elaborator.clear_drop_flag(block_start, self.path, mode);
@@ -964,11 +943,6 @@ where
964943
self.elaborator.patch().new_temp(ty, self.source_info.span)
965944
}
966945

967-
fn terminator_loc(&mut self, bb: BasicBlock) -> Location {
968-
let body = self.elaborator.body();
969-
self.elaborator.patch().terminator_loc(body, bb)
970-
}
971-
972946
fn constant_usize(&self, val: u16) -> Operand<'tcx> {
973947
Operand::Constant(box Constant {
974948
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-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-drop_in_place.std__vec__Vec_i32_.AddMovesForPackedDrops.before.mir
6966

0 commit comments

Comments
 (0)