|
| 1 | +use rustc_index::{bit_set::BitSet, IndexVec}; |
| 2 | +use rustc_middle::bug; |
| 3 | +use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor}; |
| 4 | +use rustc_middle::mir::*; |
| 5 | +use rustc_middle::ty::TyCtxt; |
| 6 | + |
| 7 | +/// Various parts of MIR building introduce temporaries that are commonly not needed. |
| 8 | +/// |
| 9 | +/// Notably, `if CONST` and `match CONST` end up being used-once temporaries, which |
| 10 | +/// obfuscates the structure for other passes and codegen, which would like to always |
| 11 | +/// be able to just see the constant directly. |
| 12 | +/// |
| 13 | +/// At higher optimization levels fancier passes like GVN will take care of this |
| 14 | +/// in a more general fashion, but this handles the easy cases so can run in debug. |
| 15 | +/// |
| 16 | +/// This only removes constants with a single-use because re-evaluating constants |
| 17 | +/// isn't always an improvement, especially for large ones. |
| 18 | +/// |
| 19 | +/// It also removes *never*-used constants, since it had all the information |
| 20 | +/// needed to do that too, including updating the debug info. |
| 21 | +pub struct SingleUseConsts; |
| 22 | + |
| 23 | +impl<'tcx> MirPass<'tcx> for SingleUseConsts { |
| 24 | + fn is_enabled(&self, sess: &rustc_session::Session) -> bool { |
| 25 | + sess.mir_opt_level() > 0 |
| 26 | + } |
| 27 | + |
| 28 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 29 | + let mut finder = SingleUseConstsFinder { |
| 30 | + ineligible_locals: BitSet::new_empty(body.local_decls.len()), |
| 31 | + locations: IndexVec::new(), |
| 32 | + }; |
| 33 | + |
| 34 | + finder.ineligible_locals.insert_range(..=Local::from_usize(body.arg_count)); |
| 35 | + |
| 36 | + finder.visit_body(body); |
| 37 | + |
| 38 | + for (local, locations) in finder.locations.iter_enumerated() { |
| 39 | + if finder.ineligible_locals.contains(local) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + let Some(init_loc) = locations.init_loc else { |
| 44 | + continue; |
| 45 | + }; |
| 46 | + |
| 47 | + // We're only changing an operand, not the terminator kinds or successors |
| 48 | + let basic_blocks = body.basic_blocks.as_mut_preserves_cfg(); |
| 49 | + let init_statement = |
| 50 | + basic_blocks[init_loc.block].statements[init_loc.statement_index].replace_nop(); |
| 51 | + let StatementKind::Assign(place_and_rvalue) = init_statement.kind else { |
| 52 | + bug!("No longer an assign?"); |
| 53 | + }; |
| 54 | + let (place, rvalue) = *place_and_rvalue; |
| 55 | + assert_eq!(place.as_local(), Some(local)); |
| 56 | + let Rvalue::Use(operand) = rvalue else { bug!("No longer a use?") }; |
| 57 | + |
| 58 | + let mut replacer = LocalReplacer { tcx, local, operand: Some(operand) }; |
| 59 | + |
| 60 | + for var_debug_info in &mut body.var_debug_info { |
| 61 | + replacer.visit_var_debug_info(var_debug_info); |
| 62 | + } |
| 63 | + |
| 64 | + let Some(use_loc) = locations.use_loc else { continue }; |
| 65 | + |
| 66 | + let use_block = &mut basic_blocks[use_loc.block]; |
| 67 | + if let Some(use_statement) = use_block.statements.get_mut(use_loc.statement_index) { |
| 68 | + replacer.visit_statement(use_statement, use_loc); |
| 69 | + } else { |
| 70 | + replacer.visit_terminator(use_block.terminator_mut(), use_loc); |
| 71 | + } |
| 72 | + |
| 73 | + if replacer.operand.is_some() { |
| 74 | + bug!( |
| 75 | + "operand wasn't used replacing local {local:?} with locations {locations:?} in body {body:#?}" |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[derive(Copy, Clone, Debug)] |
| 83 | +struct LocationPair { |
| 84 | + init_loc: Option<Location>, |
| 85 | + use_loc: Option<Location>, |
| 86 | +} |
| 87 | + |
| 88 | +impl LocationPair { |
| 89 | + fn new() -> Self { |
| 90 | + Self { init_loc: None, use_loc: None } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +struct SingleUseConstsFinder { |
| 95 | + ineligible_locals: BitSet<Local>, |
| 96 | + locations: IndexVec<Local, LocationPair>, |
| 97 | +} |
| 98 | + |
| 99 | +impl<'tcx> Visitor<'tcx> for SingleUseConstsFinder { |
| 100 | + fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) { |
| 101 | + if let Some(local) = place.as_local() |
| 102 | + && let Rvalue::Use(operand) = rvalue |
| 103 | + && let Operand::Constant(_) = operand |
| 104 | + { |
| 105 | + let locations = self.locations.ensure_contains_elem(local, LocationPair::new); |
| 106 | + if locations.init_loc.is_some() { |
| 107 | + self.ineligible_locals.insert(local); |
| 108 | + } else { |
| 109 | + locations.init_loc = Some(location); |
| 110 | + } |
| 111 | + } else { |
| 112 | + self.super_assign(place, rvalue, location); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) { |
| 117 | + if let Some(place) = operand.place() |
| 118 | + && let Some(local) = place.as_local() |
| 119 | + { |
| 120 | + let locations = self.locations.ensure_contains_elem(local, LocationPair::new); |
| 121 | + if locations.use_loc.is_some() { |
| 122 | + self.ineligible_locals.insert(local); |
| 123 | + } else { |
| 124 | + locations.use_loc = Some(location); |
| 125 | + } |
| 126 | + } else { |
| 127 | + self.super_operand(operand, location); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) { |
| 132 | + match &statement.kind { |
| 133 | + // Storage markers are irrelevant to this. |
| 134 | + StatementKind::StorageLive(_) | StatementKind::StorageDead(_) => {} |
| 135 | + _ => self.super_statement(statement, location), |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + fn visit_var_debug_info(&mut self, var_debug_info: &VarDebugInfo<'tcx>) { |
| 140 | + if let VarDebugInfoContents::Place(place) = &var_debug_info.value |
| 141 | + && let Some(_local) = place.as_local() |
| 142 | + { |
| 143 | + // It's a simple one that we can easily update |
| 144 | + } else { |
| 145 | + self.super_var_debug_info(var_debug_info); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + fn visit_local(&mut self, local: Local, _context: PlaceContext, _location: Location) { |
| 150 | + // If there's any path that gets here, rather than being understood elsewhere, |
| 151 | + // then we'd better not do anything with this local. |
| 152 | + self.ineligible_locals.insert(local); |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +struct LocalReplacer<'tcx> { |
| 157 | + tcx: TyCtxt<'tcx>, |
| 158 | + local: Local, |
| 159 | + operand: Option<Operand<'tcx>>, |
| 160 | +} |
| 161 | + |
| 162 | +impl<'tcx> MutVisitor<'tcx> for LocalReplacer<'tcx> { |
| 163 | + fn tcx(&self) -> TyCtxt<'tcx> { |
| 164 | + self.tcx |
| 165 | + } |
| 166 | + |
| 167 | + fn visit_operand(&mut self, operand: &mut Operand<'tcx>, _location: Location) { |
| 168 | + if let Operand::Copy(place) | Operand::Move(place) = operand |
| 169 | + && let Some(local) = place.as_local() |
| 170 | + && local == self.local |
| 171 | + { |
| 172 | + *operand = self.operand.take().unwrap_or_else(|| { |
| 173 | + bug!("there was a second use of the operand"); |
| 174 | + }); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + fn visit_var_debug_info(&mut self, var_debug_info: &mut VarDebugInfo<'tcx>) { |
| 179 | + if let VarDebugInfoContents::Place(place) = &var_debug_info.value |
| 180 | + && let Some(local) = place.as_local() |
| 181 | + && local == self.local |
| 182 | + { |
| 183 | + let const_op = self |
| 184 | + .operand |
| 185 | + .as_ref() |
| 186 | + .unwrap_or_else(|| { |
| 187 | + bug!("the operand was already stolen"); |
| 188 | + }) |
| 189 | + .constant() |
| 190 | + .unwrap() |
| 191 | + .clone(); |
| 192 | + var_debug_info.value = VarDebugInfoContents::Const(const_op); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments