]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/const_goto.rs
Rollup merge of #92611 - Amanieu:asm-reference, r=m-ou-se
[rust.git] / compiler / rustc_mir_transform / src / const_goto.rs
1 //! This pass optimizes the following sequence
2 //! ```rust,ignore (example)
3 //! bb2: {
4 //!     _2 = const true;
5 //!     goto -> bb3;
6 //! }
7 //!
8 //! bb3: {
9 //!     switchInt(_2) -> [false: bb4, otherwise: bb5];
10 //! }
11 //! ```
12 //! into
13 //! ```rust,ignore (example)
14 //! bb2: {
15 //!     _2 = const true;
16 //!     goto -> bb5;
17 //! }
18 //! ```
19
20 use crate::MirPass;
21 use rustc_middle::mir::*;
22 use rustc_middle::ty::TyCtxt;
23 use rustc_middle::{mir::visit::Visitor, ty::ParamEnv};
24
25 use super::simplify::{simplify_cfg, simplify_locals};
26
27 pub struct ConstGoto;
28
29 impl<'tcx> MirPass<'tcx> for ConstGoto {
30     fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
31         sess.mir_opt_level() >= 4
32     }
33
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());
37         let mut opt_finder =
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;
46         }
47
48         // if we applied optimizations, we potentially have some cfg to cleanup to
49         // make it easier for further passes
50         if should_simplify {
51             simplify_cfg(tcx, body);
52             simplify_locals(body, tcx);
53         }
54     }
55 }
56
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()?;
63
64             if let (place, Rvalue::Use(Operand::Constant(_const))) =
65                 last_statement.kind.as_assign()?
66             {
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];
70
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() {
76                     None?
77                 }
78
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.
84                     let 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,
90                     });
91                 }
92             }
93             Some(())
94         };
95
96         self.super_terminator(terminator, location);
97     }
98 }
99
100 struct OptimizationToApply {
101     bb_with_goto: BasicBlock,
102     target_to_use_in_goto: BasicBlock,
103 }
104
105 pub struct ConstGotoOptimizationFinder<'a, 'tcx> {
106     tcx: TyCtxt<'tcx>,
107     body: &'a Body<'tcx>,
108     param_env: ParamEnv<'tcx>,
109     optimizations: Vec<OptimizationToApply>,
110 }