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 terminator = body.basic_blocks_mut()[opt.bb_with_goto].terminator_mut();
43 let new_goto = TerminatorKind::Goto { target: opt.target_to_use_in_goto };
44 debug!("SUCCESS: replacing `{:?}` with `{:?}`", terminator.kind, new_goto);
45 terminator.kind = new_goto;
48 // if we applied optimizations, we potentially have some cfg to cleanup to
49 // make it easier for further passes
51 simplify_cfg(tcx, body);
52 simplify_locals(body, tcx);
57 impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
58 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
59 let _: Option<_> = try {
60 let target = terminator.kind.as_goto()?;
61 // We only apply this optimization if the last statement is a const assignment
62 let last_statement = self.body.basic_blocks()[location.block].statements.last()?;
64 if let (place, Rvalue::Use(Operand::Constant(_const))) =
65 last_statement.kind.as_assign()?
67 // We found a constant being assigned to `place`.
68 // Now check that the target of this Goto switches on this place.
69 let target_bb = &self.body.basic_blocks()[target];
71 // FIXME(simonvandel): We are conservative here when we don't allow
72 // any statements in the target basic block.
73 // This could probably be relaxed to allow `StorageDead`s which could be
74 // copied to the predecessor of this block.
75 if !target_bb.statements.is_empty() {
79 let target_bb_terminator = target_bb.terminator();
80 let (discr, switch_ty, targets) = target_bb_terminator.kind.as_switch()?;
81 if discr.place() == Some(*place) {
82 // We now know that the Switch matches on the const place, and it is statementless
83 // Now find which value in the Switch matches the const value.
85 _const.literal.try_eval_bits(self.tcx, self.param_env, switch_ty)?;
86 let target_to_use_in_goto = targets.target_for_value(const_value);
87 self.optimizations.push(OptimizationToApply {
88 bb_with_goto: location.block,
89 target_to_use_in_goto,
96 self.super_terminator(terminator, location);
100 struct OptimizationToApply {
101 bb_with_goto: BasicBlock,
102 target_to_use_in_goto: BasicBlock,
105 pub struct ConstGotoOptimizationFinder<'a, 'tcx> {
107 body: &'a Body<'tcx>,
108 param_env: ParamEnv<'tcx>,
109 optimizations: Vec<OptimizationToApply>,