Skip to content

Commit 15beeeb

Browse files
committed
Temp: remove unnecessary drop flags after monomorphization
1 parent 4d62cc6 commit 15beeeb

File tree

16 files changed

+116
-39
lines changed

16 files changed

+116
-39
lines changed

src/librustc/mir/mod.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,8 +1088,18 @@ pub enum TerminatorKind<'tcx> {
10881088
/// Indicates a terminator that can never be reached.
10891089
Unreachable,
10901090

1091-
/// Drop the `Place`.
1092-
Drop { location: Place<'tcx>, target: BasicBlock, unwind: Option<BasicBlock> },
1091+
/// Drop the `Place`, possibly conditioned on a flag being true.
1092+
Drop {
1093+
location: Place<'tcx>,
1094+
/// Whether to drop the value.
1095+
///
1096+
/// Before drop elaboration this is always `None. After drop elaboration
1097+
/// If this is `None` then the drop is unconditional, otherwise the drop
1098+
/// is only evaluated when the flag is true.
1099+
flag: Option<Place<'tcx>>,
1100+
target: BasicBlock,
1101+
unwind: Option<BasicBlock>,
1102+
},
10931103

10941104
/// Drop the `Place` and assign the new value over it. This ensures
10951105
/// that the assignment to `P` occurs *even if* the destructor for
@@ -1464,7 +1474,10 @@ impl<'tcx> TerminatorKind<'tcx> {
14641474
Abort => write!(fmt, "abort"),
14651475
Yield { ref value, .. } => write!(fmt, "_1 = suspend({:?})", value),
14661476
Unreachable => write!(fmt, "unreachable"),
1467-
Drop { ref location, .. } => write!(fmt, "drop({:?})", location),
1477+
Drop { ref location, flag: Some(ref flag), .. } => {
1478+
write!(fmt, "if {:?} drop({:?})", flag, location)
1479+
}
1480+
Drop { ref location, flag: None, .. } => write!(fmt, "drop({:?})", location),
14681481
DropAndReplace { ref location, ref value, .. } => {
14691482
write!(fmt, "replace({:?} <- {:?})", location, value)
14701483
}
@@ -2967,8 +2980,13 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
29672980
values: values.clone(),
29682981
targets: targets.clone(),
29692982
},
2970-
Drop { ref location, target, unwind } => {
2971-
Drop { location: location.fold_with(folder), target, unwind }
2983+
Drop { ref location, ref flag, target, unwind } => {
2984+
Drop {
2985+
location: location.fold_with(folder),
2986+
flag: flag.fold_with(folder),
2987+
target,
2988+
unwind,
2989+
}
29722990
}
29732991
DropAndReplace { ref location, ref value, target, unwind } => DropAndReplace {
29742992
location: location.fold_with(folder),
@@ -3025,7 +3043,9 @@ impl<'tcx> TypeFoldable<'tcx> for Terminator<'tcx> {
30253043
SwitchInt { ref discr, switch_ty, .. } => {
30263044
discr.visit_with(visitor) || switch_ty.visit_with(visitor)
30273045
}
3028-
Drop { ref location, .. } => location.visit_with(visitor),
3046+
Drop { ref location, ref flag, .. } => {
3047+
location.visit_with(visitor) || flag.visit_with(visitor)
3048+
},
30293049
DropAndReplace { ref location, ref value, .. } => {
30303050
location.visit_with(visitor) || value.visit_with(visitor)
30313051
}

src/librustc/mir/visit.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,22 @@ macro_rules! make_mir_visitor {
432432

433433
TerminatorKind::Drop {
434434
location,
435+
flag,
435436
target: _,
436437
unwind: _,
437438
} => {
438439
self.visit_place(
439440
location,
440441
PlaceContext::MutatingUse(MutatingUseContext::Drop),
441-
source_location
442+
source_location,
442443
);
444+
if let Some(flag) = flag {
445+
self.visit_place(
446+
flag,
447+
PlaceContext::NonMutatingUse(NonMutatingUseContext::Inspect),
448+
source_location,
449+
);
450+
}
443451
}
444452

445453
TerminatorKind::DropAndReplace {

src/librustc_codegen_ssa/mir/block.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
321321
helper: TerminatorCodegenHelper<'b, 'tcx>,
322322
mut bx: Bx,
323323
location: &mir::Place<'tcx>,
324+
flag: &Option<mir::Place<'tcx>>,
324325
target: mir::BasicBlock,
325326
unwind: Option<mir::BasicBlock>,
327+
source_info: mir::SourceInfo,
326328
) {
327329
let ty = location.ty(self.mir, bx.tcx()).ty;
328330
let ty = self.monomorphize(&ty);
@@ -335,6 +337,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
335337
return
336338
}
337339

340+
if let Some(flag) = flag {
341+
let flag = self.codegen_consume(&mut bx, &flag.as_ref()).immediate();
342+
let lltarget = helper.llblock(self, target);
343+
let drop_block = self.new_block("drop");
344+
helper.maybe_sideeffect(self.mir, &mut bx, &[target]);
345+
bx.cond_br(flag, drop_block.llbb(), lltarget);
346+
bx = drop_block;
347+
self.set_debug_loc(&mut bx, source_info);
348+
}
349+
338350
let place = self.codegen_place(&mut bx, &location.as_ref());
339351
let (args1, args2);
340352
let mut args = if let Some(llextra) = place.llextra {
@@ -854,8 +866,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
854866
bx.unreachable();
855867
}
856868

857-
mir::TerminatorKind::Drop { ref location, target, unwind } => {
858-
self.codegen_drop_terminator(helper, bx, location, target, unwind);
869+
mir::TerminatorKind::Drop { ref location, ref flag, target, unwind } => {
870+
self.codegen_drop_terminator(
871+
helper,
872+
bx,
873+
location,
874+
flag,
875+
target,
876+
unwind,
877+
terminator.source_info,
878+
);
859879
}
860880

861881
mir::TerminatorKind::Assert { ref cond, expected, ref msg, target, cleanup } => {

src/librustc_mir/borrow_check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ impl<'cx, 'tcx> DataflowResultsConsumer<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tcx
618618
}
619619
TerminatorKind::Drop {
620620
location: ref drop_place,
621+
flag: _,
621622
target: _,
622623
unwind: _,
623624
} => {

src/librustc_mir/borrow_check/nll/invalidation.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
155155
}
156156
TerminatorKind::Drop {
157157
location: ref drop_place,
158+
flag: _,
158159
target: _,
159160
unwind: _,
160161
} => {

src/librustc_mir/build/scope.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ impl DropTree {
352352
DropKind::Value => {
353353
let terminator = TerminatorKind::Drop {
354354
target: blocks[drop_data.1].unwrap(),
355+
flag: None,
355356
// The caller will handle this if needed.
356357
unwind: None,
357358
location: drop_data.0.local.into(),
@@ -1250,6 +1251,7 @@ fn build_scope_drops<'tcx>(
12501251
let next = cfg.start_new_block();
12511252
cfg.terminate(block, source_info, TerminatorKind::Drop {
12521253
location: local.into(),
1254+
flag: None,
12531255
target: next,
12541256
unwind: None
12551257
});

src/librustc_mir/dataflow/generic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ where
480480
mir::TerminatorKind::Goto { target }
481481
| mir::TerminatorKind::Assert { target, cleanup: None, .. }
482482
| mir::TerminatorKind::Yield { resume: target, drop: None, .. }
483-
| mir::TerminatorKind::Drop { target, location: _, unwind: None }
483+
| mir::TerminatorKind::Drop { target, location: _, unwind: None, flag: _ }
484484
| mir::TerminatorKind::DropAndReplace { target, value: _, location: _, unwind: None } =>
485485
{
486486
self.propagate_bits_into_entry_set_for(in_out, target, dirty_list);
@@ -492,7 +492,7 @@ where
492492
}
493493

494494
mir::TerminatorKind::Assert { target, cleanup: Some(unwind), .. }
495-
| mir::TerminatorKind::Drop { target, location: _, unwind: Some(unwind) }
495+
| mir::TerminatorKind::Drop { target, location: _, unwind: Some(unwind), flag: _ }
496496
| mir::TerminatorKind::DropAndReplace {
497497
target,
498498
value: _,

src/librustc_mir/dataflow/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ where
840840
mir::TerminatorKind::Goto { target } |
841841
mir::TerminatorKind::Assert { target, cleanup: None, .. } |
842842
mir::TerminatorKind::Yield { resume: target, drop: None, .. } |
843-
mir::TerminatorKind::Drop { target, location: _, unwind: None } |
843+
mir::TerminatorKind::Drop { target, location: _, unwind: None, flag: _ } |
844844
mir::TerminatorKind::DropAndReplace {
845845
target, value: _, location: _, unwind: None
846846
} => {
@@ -851,7 +851,7 @@ where
851851
self.propagate_bits_into_entry_set_for(in_out, drop, dirty_list);
852852
}
853853
mir::TerminatorKind::Assert { target, cleanup: Some(unwind), .. } |
854-
mir::TerminatorKind::Drop { target, location: _, unwind: Some(unwind) } |
854+
mir::TerminatorKind::Drop { target, location: _, unwind: Some(unwind), flag: _ } |
855855
mir::TerminatorKind::DropAndReplace {
856856
target, value: _, location: _, unwind: Some(unwind)
857857
} => {

src/librustc_mir/dataflow/move_paths/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
368368
self.gather_operand(value);
369369
}
370370

371-
TerminatorKind::Drop { ref location, target: _, unwind: _ } => {
371+
TerminatorKind::Drop { ref location, target: _, unwind: _, flag: _ } => {
372372
self.gather_move(location);
373373
}
374374
TerminatorKind::DropAndReplace { ref location, ref value, .. } => {

src/librustc_mir/interpret/terminator.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
105105

106106
Drop {
107107
ref location,
108+
ref flag,
108109
target,
109110
unwind,
110111
} => {
@@ -113,14 +114,22 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
113114
let ty = place.layout.ty;
114115
trace!("TerminatorKind::drop: {:?}, type {}", location, ty);
115116

116-
let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
117-
self.drop_in_place(
118-
place,
119-
instance,
120-
terminator.source_info.span,
121-
target,
122-
unwind
123-
)?;
117+
let should_drop = if let Some(flag) = flag {
118+
let imm = self.read_scalar(self.eval_place_to_op(flag, None)?)?.not_undef()?;
119+
imm.to_bool()?
120+
} else {
121+
true
122+
};
123+
if should_drop {
124+
let instance = Instance::resolve_drop_in_place(*self.tcx, ty);
125+
self.drop_in_place(
126+
place,
127+
instance,
128+
terminator.source_info.span,
129+
target,
130+
unwind
131+
)?;
132+
}
124133
}
125134

126135
Assert {

src/librustc_mir/shim.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl<'a, 'tcx> DropElaborator<'a, 'tcx> for DropShimElaborator<'a, 'tcx> {
281281
}
282282
}
283283

284-
fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Operand<'tcx>> {
284+
fn get_drop_flag(&mut self, _path: Self::Path) -> Option<Place<'tcx>> {
285285
None
286286
}
287287

@@ -617,6 +617,7 @@ impl CloneShimBuilder<'tcx> {
617617
// `drop(dest[beg])`;
618618
self.block(vec![], TerminatorKind::Drop {
619619
location: self.tcx.mk_place_index(dest, beg),
620+
flag: None,
620621
target: BasicBlock::new(8),
621622
unwind: None,
622623
}, true);
@@ -674,6 +675,7 @@ impl CloneShimBuilder<'tcx> {
674675
// Drop previous field and goto previous cleanup block.
675676
self.block(vec![], TerminatorKind::Drop {
676677
location: previous_field,
678+
flag: None,
677679
target: previous_cleanup,
678680
unwind: None,
679681
}, true);
@@ -808,6 +810,7 @@ fn build_call_shim<'tcx>(
808810
// BB #1 - drop for Self
809811
block(&mut blocks, vec![], TerminatorKind::Drop {
810812
location: Place::from(rcvr_arg),
813+
flag: None,
811814
target: BasicBlock::new(2),
812815
unwind: None
813816
}, false);
@@ -818,6 +821,7 @@ fn build_call_shim<'tcx>(
818821
// BB #3 - drop if closure panics
819822
block(&mut blocks, vec![], TerminatorKind::Drop {
820823
location: Place::from(rcvr_arg),
824+
flag: None,
821825
target: BasicBlock::new(4),
822826
unwind: None
823827
}, true);

src/librustc_mir/transform/add_moves_for_packed_drops.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ fn add_move_for_packed_drop<'tcx>(
9090
is_cleanup: bool,
9191
) {
9292
debug!("add_move_for_packed_drop({:?} @ {:?})", terminator, loc);
93-
let (location, target, unwind) = match terminator.kind {
94-
TerminatorKind::Drop { ref location, target, unwind } =>
95-
(location, target, unwind),
93+
let (location, flag, target, unwind) = match terminator.kind {
94+
TerminatorKind::Drop { ref location, ref flag, target, unwind } =>
95+
(location, flag, target, unwind),
9696
_ => unreachable!()
9797
};
9898

@@ -116,6 +116,7 @@ fn add_move_for_packed_drop<'tcx>(
116116
Rvalue::Use(Operand::Move(location.clone())));
117117
patch.patch_terminator(loc.block, TerminatorKind::Drop {
118118
location: Place::from(temp),
119+
flag: flag.clone(),
119120
target: storage_dead_block,
120121
unwind
121122
});

src/librustc_mir/transform/elaborate_drops.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ impl<'a, 'b, 'tcx> DropElaborator<'a, 'tcx> for Elaborator<'a, 'b, 'tcx> {
257257
})
258258
}
259259

260-
fn get_drop_flag(&mut self, path: Self::Path) -> Option<Operand<'tcx>> {
261-
self.ctxt.drop_flag(path).map(Operand::Copy)
260+
fn get_drop_flag(&mut self, path: Self::Path) -> Option<Place<'tcx>> {
261+
self.ctxt.drop_flag(path)
262262
}
263263
}
264264

@@ -374,10 +374,14 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
374374

375375
let resume_block = self.patch.resume_block();
376376
match terminator.kind {
377-
TerminatorKind::Drop { ref location, target, unwind } => {
377+
TerminatorKind::Drop { ref location, ref flag, target, unwind } => {
378378
let init_data = self.initialization_data_at(loc);
379379
match self.move_data().rev_lookup.find(location.as_ref()) {
380380
LookupResult::Exact(path) => {
381+
debug_assert!(
382+
flag.is_none(),
383+
"should not have drop flags before elaboration",
384+
);
381385
elaborate_drop(
382386
&mut Elaborator {
383387
init_data: &init_data,
@@ -494,6 +498,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
494498
debug!("elaborate_drop_and_replace({:?}) - untracked {:?}", terminator, parent);
495499
self.patch.patch_terminator(bb, TerminatorKind::Drop {
496500
location: location.clone(),
501+
flag: None,
497502
target,
498503
unwind: Some(unwind)
499504
});

src/librustc_mir/transform/generator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ fn elaborate_generator_drops<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, body: &mut
894894
source_info,
895895
kind: TerminatorKind::Drop {
896896
location,
897+
flag: None,
897898
target,
898899
unwind
899900
}
@@ -1109,6 +1110,7 @@ fn insert_clean_drop(body: &mut Body<'_>) -> BasicBlock {
11091110
let drop_clean = BasicBlock::new(body.basic_blocks().len());
11101111
let term = TerminatorKind::Drop {
11111112
location: Place::from(self_arg()),
1113+
flag: None,
11121114
target: return_block,
11131115
unwind: None,
11141116
};

src/librustc_mir/transform/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl Inliner<'tcx> {
303303
let term = blk.terminator();
304304
let mut is_drop = false;
305305
match term.kind {
306-
TerminatorKind::Drop { ref location, target, unwind } |
306+
TerminatorKind::Drop { ref location, target, unwind, .. } |
307307
TerminatorKind::DropAndReplace { ref location, target, unwind, .. } => {
308308
is_drop = true;
309309
work_list.push(target);

0 commit comments

Comments
 (0)