Debian Codegraph @

lib/mlrisc-lib/MLRISC/ir-archive/derived-graph.sml

raw
1 (*
2 * Compute Tarjan's dominator derived graph from a dominator tree.
3 * This is used partly to computing path expressions. Alternatively,
4 * it can also be used for testing for reducibility. In particular,
5 * cycles involving more than one node represent irreducible loops
6 * in the flow graph.
7 *
8 * -- Allen
9 *)
10
11 functor DerivedGraph(Dom : DOMINATOR_TREE): DERIVED_GRAPH =
12 struct
13 structure Dom = Dom
14 structure G = Graph
15 structure GI = Dom.GI
16 structure A = Array
17
18 type ('n,'e) derived_graph = ('n,'e Graph.edge,unit) Graph.graph
19
20 fun derived_graph (Dom as G.GRAPH dom) =
21 let val N = #capacity dom ()
22 val D as G.GRAPH d = GI.graph("derived graph",(),N)
23 val G.GRAPH cfg = Dom.cfg Dom
24 val ancestors = A.array(Dom.max_levels Dom,0)
25 val levelsMap = Dom.levelsMap Dom
26 fun dfs lvl i =
27 let val _ = A.update(ancestors,lvl,i)
28 val _ = #add_node d (i,#node_info cfg i)
29 fun add_edge (e as (i,j,_)) =
30 let val level = A.sub(levelsMap,j)
31 in if lvl < level then
32 #add_edge d (i,j,e) (* i idom j ! *)
33 else
34 #add_edge d (A.sub(ancestors,level),j,e)
35 end
36 in app add_edge (#out_edges cfg i);
37 app (dfs (lvl+1)) (#succ dom i)
38 end
39
40 in app (dfs 0) (#entries dom ());
41 #set_entries d (#entries dom ());
42 D
43 end
44
45 end
46