]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_mir_transform/src/const_goto.rs
Rollup merge of #107646 - estebank:specific-span, r=compiler-errors
[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 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;
48         }
49
50         // if we applied optimizations, we potentially have some cfg to cleanup to
51         // make it easier for further passes
52         if should_simplify {
53             simplify_cfg(tcx, body);
54             simplify_locals(body, tcx);
55         }
56     }
57 }
58
59 impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> {
60     fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {
61         if data.is_cleanup {
62             // Because of the restrictions around control flow in cleanup blocks, we don't perform
63             // this optimization at all in such blocks.
64             return;
65         }
66         self.super_basic_block_data(block, data);
67     }
68
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()?;
74
75             if let (place, Rvalue::Use(Operand::Constant(_const))) =
76                 last_statement.kind.as_assign()?
77             {
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];
81
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())
88                     } else {
89                         None?;
90                     }
91                 }
92
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.
99                     let 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,
105                         stmts_move_up,
106                     });
107                 }
108             }
109             Some(())
110         };
111
112         self.super_terminator(terminator, location);
113     }
114 }
115
116 struct OptimizationToApply<'tcx> {
117     bb_with_goto: BasicBlock,
118     target_to_use_in_goto: BasicBlock,
119     stmts_move_up: Vec<Statement<'tcx>>,
120 }
121
122 pub struct ConstGotoOptimizationFinder<'a, 'tcx> {
123     tcx: TyCtxt<'tcx>,
124     body: &'a Body<'tcx>,
125     param_env: ParamEnv<'tcx>,
126     optimizations: Vec<OptimizationToApply<'tcx>>,
127 }