|
8 | 8 | // option. This file may not be copied, modified, or distributed |
9 | 9 | // except according to those terms. |
10 | 10 |
|
11 | | -use mir::repr::Mir; |
| 11 | +use mir::repr::{Mir, BasicBlockData, BasicBlock}; |
| 12 | +use mir::mir_map::MirMap; |
12 | 13 | use middle::ty::ctxt; |
13 | 14 |
|
14 | | -pub trait MirPass { |
15 | | - fn run_on_mir<'tcx>(&mut self, mir: &mut Mir<'tcx>, tcx: &ctxt<'tcx>); |
| 15 | +/// Contains various metadata about the pass. |
| 16 | +pub trait Pass { |
| 17 | + /// Ordering of the pass. Lower value runs the pass earlier. |
| 18 | + fn priority(&self) -> usize; |
| 19 | + // Possibly also `fn name()` and `fn should_run(Session)` etc. |
| 20 | +} |
| 21 | + |
| 22 | +/// Pass which inspects the whole MirMap. |
| 23 | +pub trait MirMapPass: Pass { |
| 24 | + fn run_pass<'tcx>(&mut self, tcx: &ctxt<'tcx>, map: &mut MirMap<'tcx>); |
| 25 | +} |
| 26 | + |
| 27 | +/// Pass which only inspects MIR of distinct functions. |
| 28 | +pub trait MirPass: Pass { |
| 29 | + fn run_pass<'tcx>(&mut self, tcx: &ctxt<'tcx>, mir: &mut Mir<'tcx>); |
| 30 | +} |
| 31 | + |
| 32 | +/// Pass which only inspects basic blocks in MIR. |
| 33 | +/// |
| 34 | +/// Invariant: The blocks are considered to be fully self-contained for the purposes of this pass – |
| 35 | +/// the pass may not change the list of successors of the block or apply any transformations to |
| 36 | +/// blocks based on the information collected during earlier runs of the pass. |
| 37 | +pub trait MirBlockPass: Pass { |
| 38 | + fn run_pass<'tcx>(&mut self, tcx: &ctxt<'tcx>, bb: BasicBlock, data: &mut BasicBlockData<'tcx>); |
| 39 | +} |
| 40 | + |
| 41 | +impl<T: MirBlockPass> MirPass for T { |
| 42 | + fn run_pass<'tcx>(&mut self, tcx: &ctxt<'tcx>, mir: &mut Mir<'tcx>) { |
| 43 | + for (i, basic_block) in mir.basic_blocks.iter_mut().enumerate() { |
| 44 | + MirBlockPass::run_pass(self, tcx, BasicBlock::new(i), basic_block); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl<T: MirPass> MirMapPass for T { |
| 50 | + fn run_pass<'tcx>(&mut self, tcx: &ctxt<'tcx>, map: &mut MirMap<'tcx>) { |
| 51 | + for (_, mir) in &mut map.map { |
| 52 | + MirPass::run_pass(self, tcx, mir); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/// A manager for MIR passes. |
| 58 | +pub struct Passes { |
| 59 | + passes: Vec<Box<MirMapPass>> |
| 60 | +} |
| 61 | + |
| 62 | +impl Passes { |
| 63 | + pub fn new() -> Passes { |
| 64 | + let passes = Passes { |
| 65 | + passes: Vec::new() |
| 66 | + }; |
| 67 | + passes |
| 68 | + } |
| 69 | + |
| 70 | + pub fn run_passes<'tcx>(&mut self, tcx: &ctxt<'tcx>, map: &mut MirMap<'tcx>) { |
| 71 | + self.passes.sort_by_key(|e| e.priority()); |
| 72 | + for pass in &mut self.passes { |
| 73 | + pass.run_pass(tcx, map); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + pub fn push_pass(&mut self, pass: Box<MirMapPass>) { |
| 78 | + self.passes.push(pass); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl ::std::iter::Extend<Box<MirMapPass>> for Passes { |
| 83 | + fn extend<I: IntoIterator<Item=Box<MirMapPass>>>(&mut self, it: I) { |
| 84 | + self.passes.extend(it); |
| 85 | + } |
16 | 86 | } |
0 commit comments