2020-10-03 04:18:24 -05:00
|
|
|
//! This pass optimizes the following sequence
|
|
|
|
//! ```rust,ignore (example)
|
|
|
|
//! bb2: {
|
|
|
|
//! _2 = const true;
|
|
|
|
//! goto -> bb3;
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! bb3: {
|
|
|
|
//! switchInt(_2) -> [false: bb4, otherwise: bb5];
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//! into
|
|
|
|
//! ```rust,ignore (example)
|
|
|
|
//! bb2: {
|
|
|
|
//! _2 = const true;
|
|
|
|
//! goto -> bb5;
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
|
2020-12-31 18:53:25 -06:00
|
|
|
use crate::MirPass;
|
2020-10-03 04:18:24 -05:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
|
|
|
use rustc_middle::{mir::visit::Visitor, ty::ParamEnv};
|
|
|
|
|
|
|
|
use super::simplify::{simplify_cfg, simplify_locals};
|
|
|
|
|
|
|
|
pub struct ConstGoto;
|
|
|
|
|
|
|
|
impl<'tcx> MirPass<'tcx> for ConstGoto {
|
2021-12-02 11:17:32 -06:00
|
|
|
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
|
|
|
|
sess.mir_opt_level() >= 4
|
|
|
|
}
|
|
|
|
|
2020-10-03 04:18:24 -05:00
|
|
|
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
|
|
|
|
trace!("Running ConstGoto on {:?}", body.source);
|
|
|
|
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
|
|
|
|
let mut opt_finder =
|
|
|
|
ConstGotoOptimizationFinder { tcx, body, optimizations: vec![], param_env };
|
|
|
|
opt_finder.visit_body(body);
|
|
|
|
let should_simplify = !opt_finder.optimizations.is_empty();
|
|
|
|
for opt in opt_finder.optimizations {
|
2022-04-06 02:17:41 -05:00
|
|
|
let block = &mut body.basic_blocks_mut()[opt.bb_with_goto];
|
|
|
|
block.statements.extend(opt.stmts_move_up);
|
|
|
|
let terminator = block.terminator_mut();
|
2020-10-03 04:18:24 -05:00
|
|
|
let new_goto = TerminatorKind::Goto { target: opt.target_to_use_in_goto };
|
|
|
|
debug!("SUCCESS: replacing `{:?}` with `{:?}`", terminator.kind, new_goto);
|
|
|
|
terminator.kind = new_goto;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we applied optimizations, we potentially have some cfg to cleanup to
|
|
|
|
// make it easier for further passes
|
|
|
|
if should_simplify {
|
2021-05-01 16:56:48 -05:00
|
|
|
simplify_cfg(tcx, body);
|
2020-10-03 04:18:24 -05:00
|
|
|
simplify_locals(body, tcx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 02:48:37 -06:00
|
|
|
impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
|
2023-01-26 05:50:37 -06:00
|
|
|
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
|
|
|
|
if data.is_cleanup {
|
|
|
|
// Because of the restrictions around control flow in cleanup blocks, we don't perform
|
|
|
|
// this optimization at all in such blocks.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.super_basic_block_data(block, data);
|
|
|
|
}
|
|
|
|
|
2020-10-03 04:18:24 -05:00
|
|
|
fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
|
|
|
|
let _: Option<_> = try {
|
|
|
|
let target = terminator.kind.as_goto()?;
|
|
|
|
// We only apply this optimization if the last statement is a const assignment
|
2022-07-04 19:00:00 -05:00
|
|
|
let last_statement = self.body.basic_blocks[location.block].statements.last()?;
|
2020-10-03 04:18:24 -05:00
|
|
|
|
|
|
|
if let (place, Rvalue::Use(Operand::Constant(_const))) =
|
|
|
|
last_statement.kind.as_assign()?
|
|
|
|
{
|
|
|
|
// We found a constant being assigned to `place`.
|
|
|
|
// Now check that the target of this Goto switches on this place.
|
2022-07-04 19:00:00 -05:00
|
|
|
let target_bb = &self.body.basic_blocks[target];
|
2020-10-03 04:18:24 -05:00
|
|
|
|
2022-04-06 02:17:41 -05:00
|
|
|
// 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?;
|
|
|
|
}
|
2020-10-03 04:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let target_bb_terminator = target_bb.terminator();
|
2022-12-03 18:03:27 -06:00
|
|
|
let (discr, targets) = target_bb_terminator.kind.as_switch()?;
|
2020-10-03 04:18:24 -05:00
|
|
|
if discr.place() == Some(*place) {
|
2022-12-03 18:03:27 -06:00
|
|
|
let switch_ty = place.ty(self.body.local_decls(), self.tcx).ty;
|
2020-10-03 04:18:24 -05:00
|
|
|
// We now know that the Switch matches on the const place, and it is statementless
|
|
|
|
// Now find which value in the Switch matches the const value.
|
|
|
|
let const_value =
|
|
|
|
_const.literal.try_eval_bits(self.tcx, self.param_env, switch_ty)?;
|
2021-11-26 20:18:14 -06:00
|
|
|
let target_to_use_in_goto = targets.target_for_value(const_value);
|
2020-10-03 04:18:24 -05:00
|
|
|
self.optimizations.push(OptimizationToApply {
|
|
|
|
bb_with_goto: location.block,
|
|
|
|
target_to_use_in_goto,
|
2022-04-06 02:17:41 -05:00
|
|
|
stmts_move_up,
|
2020-10-03 04:18:24 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(())
|
|
|
|
};
|
|
|
|
|
|
|
|
self.super_terminator(terminator, location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 02:17:41 -05:00
|
|
|
struct OptimizationToApply<'tcx> {
|
2020-10-03 04:18:24 -05:00
|
|
|
bb_with_goto: BasicBlock,
|
|
|
|
target_to_use_in_goto: BasicBlock,
|
2022-04-06 02:17:41 -05:00
|
|
|
stmts_move_up: Vec<Statement<'tcx>>,
|
2020-10-03 04:18:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConstGotoOptimizationFinder<'a, 'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
body: &'a Body<'tcx>,
|
|
|
|
param_env: ParamEnv<'tcx>,
|
2022-04-06 02:17:41 -05:00
|
|
|
optimizations: Vec<OptimizationToApply<'tcx>>,
|
2020-10-03 04:18:24 -05:00
|
|
|
}
|