1 //! This pass optimizes the following sequence
2 //! ```rust,ignore (example)
9 //! switchInt(_2) -> [false: bb4, otherwise: bb5];
13 //! ```rust,ignore (example)
21 use rustc_middle::mir::*;
22 use rustc_middle::ty::TyCtxt;
23 use rustc_middle::{mir::visit::Visitor, ty::ParamEnv};
25 use super::simplify::{simplify_cfg, simplify_locals};
29 impl<'tcx> MirPass<'tcx> for ConstGoto {
30 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
31 sess.mir_opt_level() >= 4
34 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
35 trace!("Running ConstGoto on {:?}", body.source);
36 let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
38 ConstGotoOptimizationFinder { tcx, body, optimizations: vec![], param_env };
39 opt_finder.visit_body(body);
40 let should_simplify = !opt_finder.optimizations.is_empty();
41 for opt in opt_finder.optimizations {
42 let block = &mut body.basic_blocks_mut()[opt.bb_with_goto];
43 block.statements.extend(opt.stmts_move_up);
44 let terminator = block.terminator_mut();
45 let new_goto = TerminatorKind::Goto { target: opt.target_to_use_in_goto };
46 debug!("SUCCESS: replacing `{:?}` with `{:?}`", terminator.kind, new_goto);
47 terminator.kind = new_goto;
50 // if we applied optimizations, we potentially have some cfg to cleanup to
51 // make it easier for further passes
53 simplify_cfg(tcx, body);
54 simplify_locals(body, tcx);
59 impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
60 fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
62 // Because of the restrictions around control flow in cleanup blocks, we don't perform
63 // this optimization at all in such blocks.
66 self.super_basic_block_data(block, data);
69 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
70 let _: Option<_> = try {
71 let target = terminator.kind.as_goto()?;
72 // We only apply this optimization if the last statement is a const assignment
73 let last_statement = self.body.basic_blocks[location.block].statements.last()?;
75 if let (place, Rvalue::Use(Operand::Constant(_const))) =
76 last_statement.kind.as_assign()?
78 // We found a constant being assigned to `place`.
79 // Now check that the target of this Goto switches on this place.
80 let target_bb = &self.body.basic_blocks[target];
82 // The `StorageDead(..)` statement does not affect the functionality of mir.
83 // We can move this part of the statement up to the predecessor.
84 let mut stmts_move_up = Vec::new();
85 for stmt in &target_bb.statements {
86 if let StatementKind::StorageDead(..) = stmt.kind {
87 stmts_move_up.push(stmt.clone())
93 let target_bb_terminator = target_bb.terminator();
94 let (discr, targets) = target_bb_terminator.kind.as_switch()?;
95 if discr.place() == Some(*place) {
96 let switch_ty = place.ty(self.body.local_decls(), self.tcx).ty;
97 // We now know that the Switch matches on the const place, and it is statementless
98 // Now find which value in the Switch matches the const value.
100 _const.literal.try_eval_bits(self.tcx, self.param_env, switch_ty)?;
101 let target_to_use_in_goto = targets.target_for_value(const_value);
102 self.optimizations.push(OptimizationToApply {
103 bb_with_goto: location.block,
104 target_to_use_in_goto,
112 self.super_terminator(terminator, location);
116 struct OptimizationToApply<'tcx> {
117 bb_with_goto: BasicBlock,
118 target_to_use_in_goto: BasicBlock,
119 stmts_move_up: Vec<Statement<'tcx>>,
122 pub struct ConstGotoOptimizationFinder<'a, 'tcx> {
124 body: &'a Body<'tcx>,
125 param_env: ParamEnv<'tcx>,
126 optimizations: Vec<OptimizationToApply<'tcx>>,