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 {

0 commit comments

Comments
 (0)