|
| 1 | +use crate::Pass; |
| 2 | +use common::cfg::{self, Cfg, Dominators}; |
| 3 | +use dataflow_analysis::{run_analysis, DataflowAnalyses, ReachingDefinitions}; |
| 4 | +use std::collections::HashSet; |
| 5 | + |
| 6 | +pub struct LoopInvariantCodeMotionPass {} |
| 7 | + |
| 8 | +impl Pass for LoopInvariantCodeMotionPass { |
| 9 | + fn apply(&mut self, mut program: bril_rs::Program) -> bril_rs::Program { |
| 10 | + let mut output_program = cfg::convert_to_ssa(program); |
| 11 | + for function in output_program.functions.iter_mut() { |
| 12 | + function.instrs = common::cfg::convert_cfg_to_instruction_stream( |
| 13 | + self.process_cfg(Cfg::new(function)), |
| 14 | + ); |
| 15 | + } |
| 16 | + output_program |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +fn find_back_edges( |
| 21 | + cfg: &Cfg, |
| 22 | + dominators: &Dominators, |
| 23 | +) -> HashSet<(usize /*Start index*/, usize /*End index*/)> { |
| 24 | + if cfg.dag.number_of_nodes() == 0 { |
| 25 | + return HashSet::new(); |
| 26 | + } |
| 27 | + let mut back_edges = HashSet::new(); |
| 28 | + let mut visited = vec![false; cfg.dag.number_of_nodes()]; |
| 29 | + let mut nodes_to_visit = vec![0]; |
| 30 | + while !nodes_to_visit.is_empty() { |
| 31 | + let node = nodes_to_visit.pop().unwrap(); |
| 32 | + visited[node] = true; |
| 33 | + for &successor in cfg.dag.get_successor_indices(node) { |
| 34 | + // If the successor is visited and is a dominator of the current node, then it is a back edge. |
| 35 | + if visited[successor] && dominators.set_per_node[node].contains(&successor) { |
| 36 | + back_edges.insert((node, successor)); |
| 37 | + } else { |
| 38 | + nodes_to_visit.push(successor); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + back_edges |
| 43 | +} |
| 44 | + |
| 45 | +fn find_loop_nodes( |
| 46 | + cfg: &Cfg, |
| 47 | + dominators: &Dominators, |
| 48 | + loop_header: usize, |
| 49 | + seed: usize, |
| 50 | +) -> Vec<usize> { |
| 51 | + let mut loop_nodes = vec![loop_header]; |
| 52 | + let mut visited = vec![false; cfg.dag.number_of_nodes()]; |
| 53 | + visited[loop_header] = true; |
| 54 | + let mut nodes_to_visit = vec![seed]; |
| 55 | + while !nodes_to_visit.is_empty() { |
| 56 | + let node = nodes_to_visit.pop().unwrap(); |
| 57 | + visited[node] = true; |
| 58 | + loop_nodes.push(node); |
| 59 | + for &predecessor in cfg.dag.get_predecessor_indices(node) { |
| 60 | + // If the predecessor is not visited and if the loop header dominates the predecessor, then add it to the list of nodes to visit. |
| 61 | + // All nodes in the loop should have the loop header as a dominator. |
| 62 | + if !visited[predecessor] && dominators.set_per_node[predecessor].contains(&loop_header) |
| 63 | + { |
| 64 | + nodes_to_visit.push(predecessor); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + loop_nodes |
| 69 | +} |
| 70 | + |
| 71 | +impl LoopInvariantCodeMotionPass { |
| 72 | + pub fn new() -> Self { |
| 73 | + LoopInvariantCodeMotionPass {} |
| 74 | + } |
| 75 | + fn process_cfg(&mut self, cfg: Cfg) -> Cfg { |
| 76 | + // Precondition: We make the assumption that the CFG is reducible. |
| 77 | + let dominators = cfg::Dominators::new(&cfg); |
| 78 | + let (reaching_definitions_in, reaching_definitions_out) = |
| 79 | + ReachingDefinitions {}.run_analysis(&cfg, Some(false)); |
| 80 | + find_back_edges(&cfg, &dominators) |
| 81 | + .iter() |
| 82 | + .map(|(src, loop_header)| { |
| 83 | + // For each back edge, find the loop nodes. |
| 84 | + assert!( |
| 85 | + dominators.set_per_node[*src].contains(&loop_header), |
| 86 | + "{src}->{loop_header}| dominators of {} are: {:#?}", |
| 87 | + loop_header, |
| 88 | + dominators.set_per_node[*loop_header] |
| 89 | + ); |
| 90 | + ( |
| 91 | + *loop_header, |
| 92 | + find_loop_nodes(&cfg, &dominators, *loop_header, *src), |
| 93 | + ) |
| 94 | + }) |
| 95 | + .for_each( |
| 96 | + |(loop_header, loop_nodes)| // For each loop, find the loop invariant instructions. |
| 97 | + { |
| 98 | + // todo!("Find loop invariant instructions for loop: {:#?}", loop_nodes); |
| 99 | + }, |
| 100 | + ); |
| 101 | + |
| 102 | + cfg |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use crate::Pass; |
| 109 | + use bril_rs::Program; |
| 110 | + |
| 111 | + fn parse_program(text: &str) -> Program { |
| 112 | + let program = common::parse_bril_text(text); |
| 113 | + assert!(program.is_ok(), "{}", program.err().unwrap()); |
| 114 | + program.unwrap() |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_loop_invariant_code_motion() { |
| 119 | + let program = parse_program(indoc::indoc! {r#" |
| 120 | + @main { |
| 121 | + n: int = const 10; |
| 122 | + inc: int = const 5; |
| 123 | + one: int = const 1; |
| 124 | + invariant: int = const 100; |
| 125 | + i: int = const 0; |
| 126 | + sum: int = const 0; |
| 127 | + .loop: |
| 128 | + cond: bool = lt i n; |
| 129 | + br cond .body .done; |
| 130 | + .body: |
| 131 | + temp: int = add invariant inc; |
| 132 | + sum: int = add sum temp; |
| 133 | + i: int = add i one; |
| 134 | + body_cond: bool = lt temp sum; |
| 135 | + br body_cond .body_left .body_right; |
| 136 | + .body_left: |
| 137 | + jmp .body_join; |
| 138 | + .body_right: |
| 139 | + dead_store: int = const 0; |
| 140 | + jmp .body_join; |
| 141 | + .body_join: |
| 142 | + jmp .loop; |
| 143 | + .done: |
| 144 | + print sum; |
| 145 | + ret; |
| 146 | + } |
| 147 | + "#}); |
| 148 | + let optimized_program = super::LoopInvariantCodeMotionPass::new().apply(program); |
| 149 | + println!("{}", optimized_program); |
| 150 | + } |
| 151 | +} |
0 commit comments