|
| 1 | +//! These two passes provide no value to the compiler, so are off at every level. |
| 2 | +//! |
| 3 | +//! However, they can be enabled on the command line |
| 4 | +//! (`-Zmir-enable-passes=+ReorderBasicBlocks,+ReorderLocals`) |
| 5 | +//! to make the MIR easier to read for humans. |
| 6 | +
|
| 7 | +use crate::MirPass; |
| 8 | +use rustc_index::{bit_set::BitSet, IndexSlice, IndexVec}; |
| 9 | +use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor}; |
| 10 | +use rustc_middle::mir::*; |
| 11 | +use rustc_middle::ty::TyCtxt; |
| 12 | +use rustc_session::Session; |
| 13 | + |
| 14 | +/// Rearranges the basic blocks into a *reverse post-order*. |
| 15 | +/// |
| 16 | +/// Thus after this pass, all the successors of a block are later than it in the |
| 17 | +/// `IndexVec`, unless that successor is a back-edge (such as from a loop). |
| 18 | +pub struct ReorderBasicBlocks; |
| 19 | + |
| 20 | +impl<'tcx> MirPass<'tcx> for ReorderBasicBlocks { |
| 21 | + fn is_enabled(&self, _session: &Session) -> bool { |
| 22 | + false |
| 23 | + } |
| 24 | + |
| 25 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 26 | + let rpo: IndexVec<BasicBlock, BasicBlock> = |
| 27 | + body.basic_blocks.postorder().iter().copied().rev().collect(); |
| 28 | + if rpo.iter().is_sorted() { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + let mut updater = BasicBlockUpdater { map: rpo.invert_bijective_mapping(), tcx }; |
| 33 | + debug_assert_eq!(updater.map[START_BLOCK], START_BLOCK); |
| 34 | + updater.visit_body(body); |
| 35 | + |
| 36 | + permute(body.basic_blocks.as_mut(), &updater.map); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Rearranges the locals into *use* order. |
| 41 | +/// |
| 42 | +/// Thus after this pass, a local with a smaller [`Location`] where it was first |
| 43 | +/// assigned or referenced will have a smaller number. |
| 44 | +/// |
| 45 | +/// (Does not reorder arguments nor the [`RETURN_PLACE`].) |
| 46 | +pub struct ReorderLocals; |
| 47 | + |
| 48 | +impl<'tcx> MirPass<'tcx> for ReorderLocals { |
| 49 | + fn is_enabled(&self, _session: &Session) -> bool { |
| 50 | + false |
| 51 | + } |
| 52 | + |
| 53 | + fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { |
| 54 | + let mut finder = |
| 55 | + LocalFinder { map: IndexVec::new(), seen: BitSet::new_empty(body.local_decls.len()) }; |
| 56 | + |
| 57 | + // We can't reorder the return place or the arguments |
| 58 | + for local in (0..=body.arg_count).map(Local::from_usize) { |
| 59 | + finder.track(local); |
| 60 | + } |
| 61 | + |
| 62 | + for (bb, bbd) in body.basic_blocks.iter_enumerated() { |
| 63 | + finder.visit_basic_block_data(bb, bbd); |
| 64 | + } |
| 65 | + |
| 66 | + // track everything in case there are some locals that we never saw, |
| 67 | + // such as in non-block things like debug info or in non-uses. |
| 68 | + for local in body.local_decls.indices() { |
| 69 | + finder.track(local); |
| 70 | + } |
| 71 | + |
| 72 | + if finder.map.iter().is_sorted() { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + let mut updater = LocalUpdater { map: finder.map.invert_bijective_mapping(), tcx }; |
| 77 | + |
| 78 | + for local in (0..=body.arg_count).map(Local::from_usize) { |
| 79 | + debug_assert_eq!(updater.map[local], local); |
| 80 | + } |
| 81 | + |
| 82 | + updater.visit_body_preserves_cfg(body); |
| 83 | + |
| 84 | + permute(&mut body.local_decls, &updater.map); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +fn permute<I: rustc_index::Idx + Ord, T>(data: &mut IndexVec<I, T>, map: &IndexSlice<I, I>) { |
| 89 | + // FIXME: It would be nice to have a less-awkward way to apply permutations, |
| 90 | + // but I don't know one that exists. `sort_by_cached_key` has logic for it |
| 91 | + // internally, but not in a way that we're allowed to use here. |
| 92 | + let mut enumerated: Vec<_> = std::mem::take(data).into_iter_enumerated().collect(); |
| 93 | + enumerated.sort_by_key(|p| map[p.0]); |
| 94 | + *data = enumerated.into_iter().map(|p| p.1).collect(); |
| 95 | +} |
| 96 | + |
| 97 | +struct BasicBlockUpdater<'tcx> { |
| 98 | + map: IndexVec<BasicBlock, BasicBlock>, |
| 99 | + tcx: TyCtxt<'tcx>, |
| 100 | +} |
| 101 | + |
| 102 | +impl<'tcx> MutVisitor<'tcx> for BasicBlockUpdater<'tcx> { |
| 103 | + fn tcx(&self) -> TyCtxt<'tcx> { |
| 104 | + self.tcx |
| 105 | + } |
| 106 | + |
| 107 | + fn visit_terminator(&mut self, terminator: &mut Terminator<'tcx>, _location: Location) { |
| 108 | + for succ in terminator.successors_mut() { |
| 109 | + *succ = self.map[*succ]; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +struct LocalFinder { |
| 115 | + map: IndexVec<Local, Local>, |
| 116 | + seen: BitSet<Local>, |
| 117 | +} |
| 118 | + |
| 119 | +impl LocalFinder { |
| 120 | + fn track(&mut self, l: Local) { |
| 121 | + if self.seen.insert(l) { |
| 122 | + self.map.push(l); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<'tcx> Visitor<'tcx> for LocalFinder { |
| 128 | + fn visit_local(&mut self, l: Local, context: PlaceContext, _location: Location) { |
| 129 | + // Exclude non-uses to keep `StorageLive` from controlling where we put |
| 130 | + // a `Local`, since it might not actually be assigned until much later. |
| 131 | + if context.is_use() { |
| 132 | + self.track(l); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +struct LocalUpdater<'tcx> { |
| 138 | + pub map: IndexVec<Local, Local>, |
| 139 | + pub tcx: TyCtxt<'tcx>, |
| 140 | +} |
| 141 | + |
| 142 | +impl<'tcx> MutVisitor<'tcx> for LocalUpdater<'tcx> { |
| 143 | + fn tcx(&self) -> TyCtxt<'tcx> { |
| 144 | + self.tcx |
| 145 | + } |
| 146 | + |
| 147 | + fn visit_local(&mut self, l: &mut Local, _: PlaceContext, _: Location) { |
| 148 | + *l = self.map[*l]; |
| 149 | + } |
| 150 | +} |
0 commit comments