Advertisement
Guest User

Untitled

a guest
Jan 1st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.28 KB | None | 0 0
  1. impl optimization::Interprocedural for Inlining {
  2.     fn optimize<'src, 'ast>(program: &FirmProgram<'src, 'ast>) -> Outcome {
  3.         let g = CallGraph::new(program).construct();
  4.         let mut g = condensation(g, false);
  5.         g.reverse();
  6.         g.retain_edges(|g, e| {
  7.             let (n1, n2) = g.edge_endpoints(e).unwrap();
  8.             n1 != n2
  9.         });
  10.  
  11.         let topo_sort = toposort(&g, None).unwrap();
  12.         let mut map = HashMap::new();
  13.         for (idx, node_id) in topo_sort.iter().enumerate() {
  14.             map.insert(RefEq(g.node_weight(*node_id).unwrap()), idx);
  15.         }
  16.  
  17.         breakpoint!("inline graph", g, &|n: &Vec<FirmMethodP<'src, 'ast>>| {
  18.             Label::from_text(format!(
  19.                 "{:?} {}",
  20.                 n.iter()
  21.                     .map(|n| n.borrow().def.name.as_str().to_owned())
  22.                     .collect::<Vec<_>>(),
  23.                 map.get(&RefEq(n)).unwrap()
  24.             ))
  25.         });
  26.  
  27.         for node in topo_sort {
  28.             if g.node_weight(node).unwrap().len() != 1 {
  29.                 continue;
  30.             }
  31.  
  32.             for edge in g.edges_directed(node, petgraph::Direction::Outgoing) {
  33.                 Inline::inline(*edge.weight());
  34.             }
  35.         }
  36.  
  37.         Outcome::Unchanged
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement