Compute a CFG post-order when building the dominator tree.

The DominatorTree has existing DomNodes per EBB that can be used in lieu
of expensive HastSets for the depth-first traversal of the CFG.

Make the computed and cached post-order available for other passes
through the `cfg_postorder()` method which returns a slice.

The post-order algorithm is essentially the same as the one in
ControlFlowGraph::postorder_ebbs(), except it will never push a
successor node that has already been visited once. This is more
efficient, but it generates a different post-order.

Change the cfg_traversal tests to check this new algorithm.
This commit is contained in:
Jakob Stoklund Olesen
2017-06-02 15:08:02 -07:00
parent b02ccea8dc
commit 16df1f1cf5
2 changed files with 111 additions and 41 deletions

View File

@@ -1,30 +1,27 @@
extern crate cretonne;
extern crate cton_reader;
use self::cretonne::entity_map::EntityMap;
use self::cretonne::flowgraph::ControlFlowGraph;
use self::cretonne::dominator_tree::DominatorTree;
use self::cretonne::ir::Ebb;
use self::cton_reader::parse_functions;
fn test_reverse_postorder_traversal(function_source: &str, ebb_order: Vec<u32>) {
let func = &parse_functions(function_source).unwrap()[0];
let cfg = ControlFlowGraph::with_function(&func);
let ebbs = ebb_order
let domtree = DominatorTree::with_function(&func, &cfg);
let got = domtree
.cfg_postorder()
.iter()
.map(|n| Ebb::with_number(*n).unwrap())
.collect::<Vec<Ebb>>();
let mut postorder_ebbs = cfg.postorder_ebbs();
let mut postorder_map = EntityMap::with_capacity(postorder_ebbs.len());
for (i, ebb) in postorder_ebbs.iter().enumerate() {
postorder_map[ebb.clone()] = i + 1;
}
postorder_ebbs.reverse();
assert_eq!(postorder_ebbs.len(), ebbs.len());
for ebb in postorder_ebbs {
assert_eq!(ebb, ebbs[ebbs.len() - postorder_map[ebb]]);
}
.rev()
.cloned()
.collect::<Vec<_>>();
let want = ebb_order
.iter()
.map(|&n| Ebb::with_number(n).unwrap())
.collect::<Vec<_>>();
assert_eq!(got, want);
}
#[test]
@@ -53,7 +50,7 @@ fn simple_traversal() {
trap
}
",
vec![0, 2, 1, 3, 4, 5]);
vec![0, 1, 3, 2, 4, 5]);
}
#[test]
@@ -71,7 +68,7 @@ fn loops_one() {
return
}
",
vec![0, 1, 2, 3]);
vec![0, 1, 3, 2]);
}
#[test]
@@ -96,7 +93,7 @@ fn loops_two() {
return
}
",
vec![0, 1, 2, 5, 4, 3]);
vec![0, 1, 2, 4, 3, 5]);
}
#[test]
@@ -126,7 +123,7 @@ fn loops_three() {
return
}
",
vec![0, 1, 2, 5, 4, 3, 6, 7]);
vec![0, 1, 2, 4, 3, 6, 7, 5]);
}
#[test]