2020-09-13 16:04:45 +02:00
|
|
|
//! This pass replaces a drop of a type that does not need dropping, with a goto
|
|
|
|
|
2021-01-01 01:53:25 +01:00
|
|
|
use crate::MirPass;
|
2020-09-13 16:04:45 +02:00
|
|
|
use rustc_middle::mir::*;
|
2021-01-17 00:00:00 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-09-13 16:04:45 +02:00
|
|
|
|
2020-09-19 15:21:39 +02:00
|
|
|
use super::simplify::simplify_cfg;
|
|
|
|
|
2020-09-19 13:52:55 +02:00
|
|
|
pub struct RemoveUnneededDrops;
|
2020-09-13 16:04:45 +02:00
|
|
|
|
|
|
|
impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops {
|
2020-10-04 11:01:38 -07:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
trace!("Running RemoveUnneededDrops on {:?}", body.source);
|
2020-11-18 19:16:23 -05:00
|
|
|
|
2021-01-17 00:00:00 +00:00
|
|
|
let did = body.source.def_id();
|
2021-09-26 17:02:20 +02:00
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(did);
|
2021-01-17 00:00:00 +00:00
|
|
|
let mut should_simplify = false;
|
|
|
|
|
|
|
|
let (basic_blocks, local_decls) = body.basic_blocks_and_local_decls_mut();
|
|
|
|
for block in basic_blocks {
|
|
|
|
let terminator = block.terminator_mut();
|
|
|
|
if let TerminatorKind::Drop { place, target, .. } = terminator.kind {
|
|
|
|
let ty = place.ty(local_decls, tcx);
|
|
|
|
if ty.ty.needs_drop(tcx, param_env) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if !tcx.consider_optimizing(|| format!("RemoveUnneededDrops {:?} ", did)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
debug!("SUCCESS: replacing `drop` with goto({:?})", target);
|
|
|
|
terminator.kind = TerminatorKind::Goto { target };
|
|
|
|
should_simplify = true;
|
|
|
|
}
|
2020-09-13 16:04:45 +02:00
|
|
|
}
|
2020-09-19 15:21:39 +02:00
|
|
|
|
|
|
|
// if we applied optimizations, we potentially have some cfg to cleanup to
|
|
|
|
// make it easier for further passes
|
|
|
|
if should_simplify {
|
2021-05-01 14:56:48 -07:00
|
|
|
simplify_cfg(tcx, body);
|
2020-09-19 15:21:39 +02:00
|
|
|
}
|
2020-09-13 16:04:45 +02:00
|
|
|
}
|
|
|
|
}
|