Debian Codegraph @

lib/mlrisc-lib/MLRISC/IR/mlrisc-ir.sml

raw
1 (*
2 * MLRISC IR
3 *
4 * This is for performing whole program analysis.
5 * All optimizations are based on this representation.
6 * It provides a few useful views: dominator tree, control dependence graph,
7 * loop nesting (interval) structure etc. Also there is a mechanism to
8 * incrementally attach additional views to the IR. The SSA infrastructure
9 * is implemented in such a manner.
10 *
11 * -- Allen
12 *)
13
14 functor MLRISC_IR
15 (structure CFG : CONTROL_FLOW_GRAPH
16 structure CDG : CONTROL_DEPENDENCE_GRAPH
17 structure Loop : LOOP_STRUCTURE
18 structure GraphViewer : GRAPH_VIEWER
19 structure Util : CFG_UTIL
20 sharing Loop.Dom = CDG.Dom
21 sharing Util.CFG = CFG
22 ) : MLRISC_IR =
23 struct
24
25 structure I = CFG.I
26 structure CFG = CFG
27 structure Dom = Loop.Dom
28 structure CDG = CDG
29 structure Loop = Loop
30 structure G = Graph
31 structure A = Annotations
32 structure Util = Util
33 structure L = GraphLayout
34
35 type cfg = CFG.cfg
36 type IR = CFG.cfg
37 type dom = (CFG.block,CFG.edge_info,CFG.info) Dom.dominator_tree
38 type pdom = (CFG.block,CFG.edge_info,CFG.info) Dom.postdominator_tree
39 type cdg = (CFG.block,CFG.edge_info,CFG.info) CDG.cdg
40 type loop = (CFG.block,CFG.edge_info,CFG.info) Loop.loop_structure
41
42 val layouts = ref [] : (string * (IR -> L.layout)) list ref
43
44 fun addLayout name layout =
45 let fun f((x,y)::rest) = if x = name then (x,layout)::rest
46 else (x,y)::f rest
47 | f [] = [(name,layout)]
48 in layouts := f(!layouts) end
49
50 exception NoLayout
51
52 fun findLayout name =
53 let fun f [] = (print ("[Can't find "^name^"]\n"); raise NoLayout)
54 | f((x,layout)::rest) = if x = name then layout else f rest
55 in f(!layouts) end
56
57 fun view name IR = GraphViewer.view(findLayout name IR)
58 handle NoLayout => ()
59
60 fun views names IR =
61 let val layouts = map (fn n => findLayout n IR) names
62 in GraphViewer.view(GraphCombinations.sums layouts)
63 end handle NoLayout => ()
64
65 fun viewSubgraph IR subgraph =
66 GraphViewer.view (CFG.subgraphLayout{cfg=IR,subgraph=subgraph})
67
68 (*
69 * This function defines how we compute a new view
70 *)
71
72 val verbose = MLRiscControl.getFlag "verbose"
73
74 fun memo name compute =
75 let val {get,set,...} = A.new(SOME(fn _ => name))
76 fun getView(IR as G.GRAPH ir : IR)=
77 let val CFG.INFO{annotations, ...} = #graph_info ir
78 fun process(SOME(ref(SOME info))) =
79 (if !verbose then print ("[reusing "^name^"]") else (); info)
80 | process(SOME r) =
81 let val _ =
82 if !verbose then print("[computing "^name) else ()
83 val info = compute IR
84 val _ = if !verbose then print "]" else ()
85 in r := SOME info; info end
86 | process NONE =
87 let val r = ref NONE
88 fun kill() = (r := NONE;
89 if !verbose then print("[uncaching "^name^"]")
90 else ())
91 in annotations := #create CFG.CHANGED(name, kill) ::
92 set(r,!annotations);
93 process(SOME r)
94 end
95 in process(get (!annotations)) end
96 in getView
97 end
98
99 (*
100 * Extract various views from an IR
101 *)
102
103 val dom = memo "dom" Dom.makeDominator
104 val pdom = memo "pdom" Dom.makePostdominator
105 fun doms IR = (dom IR,pdom IR)
106 val cdg = memo "cdg"
107 (fn IR => CDG.control_dependence_graph CFG.cdgEdge (pdom IR))
108 val loop = memo "loop" (Loop.loop_structure o dom)
109 val changed = CFG.changed
110
111 (*
112 * Methods to layout various graphs
113 *)
114 fun defaultEdge _ = [L.COLOR "red"]
115 fun defaultGraph _ = []
116 fun layoutDom' IR G =
117 let val {node,...} = CFG.viewStyle IR
118 in L.makeLayout {edge = defaultEdge,
119 graph= defaultGraph,
120 node = node} G
121 end
122
123 fun layoutDom IR = layoutDom' IR (dom IR)
124 fun layoutPdom IR = layoutDom' IR (pdom IR)
125 fun layoutDoms IR = layoutDom' IR
126 let val (dom,pdom) = doms IR
127 in GraphCombinations.sum(dom,ReversedGraphView.rev_view pdom)
128 end
129 fun layoutCDG IR = CFG.viewLayout(cdg IR)
130 fun layoutLoop (IR as G.GRAPH cfg) =
131 let val loop = loop IR
132 val an = !(CFG.annotations IR)
133 fun mkNodes nodes =
134 String.concat(map (fn i => Int.toString i^" ") nodes)
135 fun mkEdges edges =
136 String.concat(map
137 (fn (i,j,_) => Int.toString i^"->"^Int.toString j^" ") edges)
138 fun node(_,Loop.LOOP{nesting,header,loop_nodes,
139 backedges,exits,...}) =
140 [L.LABEL
141 ("nesting: "^Int.toString nesting^"\n"^
142 CFG.show_block an (#node_info cfg header)^
143 "entry edges: "^mkEdges(Loop.entryEdges loop header)^"\n"^
144 "loop_nodes: "^mkNodes loop_nodes^"\n"^
145 "backedges: "^mkEdges backedges^"\n"^
146 "exits: "^mkEdges exits^"\n"
147 )]
148 in L.makeLayout {edge=defaultEdge,
149 graph=defaultGraph,
150 node=node} loop
151 end
152
153 (*
154 * Insert the layout methods here.
155 *)
156 val _ = addLayout "cfg" CFG.viewLayout
157 val _ = addLayout "dom" layoutDom
158 val _ = addLayout "pdom" layoutPdom
159 val _ = addLayout "doms" layoutDoms
160 val _ = addLayout "cdg" layoutCDG
161 val _ = addLayout "loop" layoutLoop
162
163 end
164