diff options
Diffstat (limited to 'compiler/rustc_mir_transform/src')
| -rw-r--r-- | compiler/rustc_mir_transform/src/const_goto.rs | 25 | ||||
| -rw-r--r-- | compiler/rustc_mir_transform/src/deref_separator.rs | 72 | ||||
| -rw-r--r-- | compiler/rustc_mir_transform/src/lib.rs | 2 |
3 files changed, 90 insertions, 9 deletions
diff --git a/compiler/rustc_mir_transform/src/const_goto.rs b/compiler/rustc_mir_transform/src/const_goto.rs index 905173b0457..5acf939f06b 100644 --- a/compiler/rustc_mir_transform/src/const_goto.rs +++ b/compiler/rustc_mir_transform/src/const_goto.rs @@ -39,7 +39,9 @@ impl<'tcx> MirPass<'tcx> for ConstGoto { opt_finder.visit_body(body); let should_simplify = !opt_finder.optimizations.is_empty(); for opt in opt_finder.optimizations { - let terminator = body.basic_blocks_mut()[opt.bb_with_goto].terminator_mut(); + let block = &mut body.basic_blocks_mut()[opt.bb_with_goto]; + block.statements.extend(opt.stmts_move_up); + let terminator = block.terminator_mut(); let new_goto = TerminatorKind::Goto { target: opt.target_to_use_in_goto }; debug!("SUCCESS: replacing `{:?}` with `{:?}`", terminator.kind, new_goto); terminator.kind = new_goto; @@ -68,12 +70,15 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { // Now check that the target of this Goto switches on this place. let target_bb = &self.body.basic_blocks()[target]; - // FIXME(simonvandel): We are conservative here when we don't allow - // any statements in the target basic block. - // This could probably be relaxed to allow `StorageDead`s which could be - // copied to the predecessor of this block. - if !target_bb.statements.is_empty() { - None? + // The `StorageDead(..)` statement does not affect the functionality of mir. + // We can move this part of the statement up to the predecessor. + let mut stmts_move_up = Vec::new(); + for stmt in &target_bb.statements { + if let StatementKind::StorageDead(..) = stmt.kind { + stmts_move_up.push(stmt.clone()) + } else { + None?; + } } let target_bb_terminator = target_bb.terminator(); @@ -87,6 +92,7 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { self.optimizations.push(OptimizationToApply { bb_with_goto: location.block, target_to_use_in_goto, + stmts_move_up, }); } } @@ -97,14 +103,15 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { } } -struct OptimizationToApply { +struct OptimizationToApply<'tcx> { bb_with_goto: BasicBlock, target_to_use_in_goto: BasicBlock, + stmts_move_up: Vec<Statement<'tcx>>, } pub struct ConstGotoOptimizationFinder<'a, 'tcx> { tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, param_env: ParamEnv<'tcx>, - optimizations: Vec<OptimizationToApply>, + optimizations: Vec<OptimizationToApply<'tcx>>, } diff --git a/compiler/rustc_mir_transform/src/deref_separator.rs b/compiler/rustc_mir_transform/src/deref_separator.rs new file mode 100644 index 00000000000..79aac163550 --- /dev/null +++ b/compiler/rustc_mir_transform/src/deref_separator.rs @@ -0,0 +1,72 @@ +use crate::MirPass; +use rustc_middle::mir::patch::MirPatch; +use rustc_middle::mir::*; +use rustc_middle::ty::TyCtxt; +pub struct Derefer; + +pub fn deref_finder<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { + let mut patch = MirPatch::new(body); + let (basic_blocks, local_decl) = body.basic_blocks_and_local_decls_mut(); + for (block, data) in basic_blocks.iter_enumerated_mut() { + for (i, stmt) in data.statements.iter_mut().enumerate() { + match stmt.kind { + StatementKind::Assign(box (og_place, Rvalue::Ref(region, borrow_knd, place))) => { + for (idx, (p_ref, p_elem)) in place.iter_projections().enumerate() { + if p_elem == ProjectionElem::Deref && !p_ref.projection.is_empty() { + // The type that we are derefing. + let ty = p_ref.ty(local_decl, tcx).ty; + let temp = patch.new_temp(ty, stmt.source_info.span); + + // Because we are assigning this right before original statement + // we are using index i of statement. + let loc = Location { block: block, statement_index: i }; + patch.add_statement(loc, StatementKind::StorageLive(temp)); + + // We are adding current p_ref's projections to our + // temp value. + let deref_place = + Place::from(p_ref.local).project_deeper(p_ref.projection, tcx); + patch.add_assign( + loc, + Place::from(temp), + Rvalue::Use(Operand::Move(deref_place)), + ); + + // We are creating a place by using our temp value's location + // and copying derefed values which we need to create new statement. + let temp_place = + Place::from(temp).project_deeper(&place.projection[idx..], tcx); + let new_stmt = Statement { + source_info: stmt.source_info, + kind: StatementKind::Assign(Box::new(( + og_place, + Rvalue::Ref(region, borrow_knd, temp_place), + ))), + }; + + // Replace current statement with newly created one. + *stmt = new_stmt; + + // Since our job with the temp is done it should be gone + let loc = Location { block: block, statement_index: i + 1 }; + patch.add_statement(loc, StatementKind::StorageDead(temp)); + + // As all projections are off the base projection, if there are + // multiple derefs in the middle of projection, it might cause + // unsoundness, to not let that happen we break the loop. + break; + } + } + } + _ => (), + } + } + } + patch.apply(body); +} + +impl<'tcx> MirPass<'tcx> for Derefer { + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { + deref_finder(tcx, body); + } +} diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index 2fca498a125..059ee09dfd7 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -53,6 +53,7 @@ mod const_prop_lint; mod coverage; mod deaggregator; mod deduplicate_blocks; +mod deref_separator; mod dest_prop; pub mod dump_mir; mod early_otherwise_branch; @@ -431,6 +432,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc // `Deaggregator` is conceptually part of MIR building, some backends rely on it happening // and it can help optimizations. &deaggregator::Deaggregator, + &deref_separator::Derefer, &Lint(const_prop_lint::ConstProp), ]; |
