Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/IL/value-numbering-fn.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/IL/value-numbering-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 740 (* value-numbering-fn.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 740 * All rights reserved.
7 :     *
8 :     * This file contains an implementation of the hash-based value numbering
9 :     * algorithm described in
10 :     *
11 :     * Value Numbering
12 :     * by Preston Briggs, Keith Cooper, and Taylor Simpson
13 :     * CRPC-TR94517-S
14 :     * November 1994
15 :     *)
16 :    
17 :     functor ValueNumberingFn (D : DOMINANCE_TREE) : sig
18 :    
19 :     structure IL : SSA
20 :    
21 :     val transform : IL.program -> IL.program
22 :    
23 :     end = struct
24 :    
25 :     structure IL = D.IL
26 : jhr 1148 structure E = ExprFn (IL)
27 :     structure ValueMap = E.Map
28 : jhr 1153 structure ST = Stats
29 : jhr 740
30 : jhr 1148 type expr = E.expr
31 : jhr 740
32 : jhr 1153 (********** Counters for statistics **********)
33 :     val cntMeaninglessPhi = ST.newCounter (IL.ilName ^ ":meaningless-phi")
34 :     val cntRedundantPhi = ST.newCounter (IL.ilName ^ ":redundant-phi")
35 :     val cntRedundantAssign = ST.newCounter (IL.ilName ^ ":redundant-assign")
36 :    
37 : jhr 1148 (* adjust a variable's use count *)
38 :     fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
39 :     fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
40 : jhr 740
41 :     local
42 :     (* property for mapping variables to their value number (VN), which is represented as a
43 :     * SSA variable. If their VN is different from themselves, then they are redundant.
44 :     *)
45 :     val {getFn=getVN, setFn=setVN, clrFn=clrVN, ...} = IL.Var.newProp (fn x => x)
46 :    
47 :     (* property for mapping value numbers to hash-consed expressions. *)
48 : jhr 1148 val {getFn=getExp : IL.var -> expr, setFn=setExp, clrFn=clrExp, ...} =
49 : jhr 1157 IL.Var.newProp (fn x => raise Fail(concat["getExp(", IL.Var.toString x, ")"]))
50 : jhr 740
51 :     datatype env = ENV of {
52 : jhr 1148 avail : IL.var ValueMap.map (* map from expressions to their value numbers, which *)
53 : jhr 740 (* are represented as SSA vars. The domain are those *)
54 :     (* expressions that are available. *)
55 :     }
56 :     in
57 : jhr 1148 val emptyEnv = ENV{avail = ValueMap.empty}
58 : jhr 740 (* map variables to their hash-consed definition *)
59 : jhr 741 val getVN = getVN
60 : jhr 1148 val setVN = setVN
61 : jhr 740 fun varToExp x = getExp(getVN x)
62 : jhr 1148 fun bindVarToExp (ENV{avail}, x, e) = (
63 : jhr 1546 (*DEBUG**Log.msg(concat["** bindVarToExp: ", IL.Var.toString x, " --> ", E.toString e, "\n"]);*)
64 : jhr 740 setVN(x, x); setExp(x, e);
65 : jhr 1148 ENV{avail = ValueMap.insert(avail, e, x)})
66 :     fun expToVN (ENV{avail}, e) = ValueMap.find(avail, e)
67 :     (* rename a variable if it's value number is different than itself *)
68 :     fun rename x = let
69 :     val x' = getVN x
70 :     in
71 :     if IL.Var.same(x, x')
72 :     then x
73 :     else (
74 : jhr 1546 (*DEBUG**Log.msg(concat["** rename ", IL.Var.toString x, " to ", IL.Var.toString x', "\n"]);*)
75 : jhr 1148 decUse x; incUse x';
76 :     x')
77 :     end
78 :     (* does a variable change? *)
79 :     fun changed x = not(IL.Var.same(x, getVN x))
80 : jhr 1153 (* clear the properties of a variable *)
81 :     fun clearVar x = (clrVN x; clrExp x)
82 :     (* clear the properties from the variables of a node *)
83 :     fun clearNode nd = List.app clearVar (IL.Node.defs nd)
84 : jhr 740 end (* local *)
85 :    
86 : jhr 1148 fun rewriteCFG cfg = let
87 :     (* in case the exit node get rewritten, we need to reset it *)
88 :     val exitNd = ref(IL.CFG.exit cfg)
89 :     (* rewrite or delete a node, if necessary. Note that we have already rewritten the JOIN nodes *)
90 :     fun doNode nd = (case IL.Node.kind nd
91 :     of IL.COND{pred, cond, trueBranch, falseBranch} =>
92 :     if changed cond
93 :     then let
94 :     val newNd = IL.Node.mkCOND {
95 :     cond = rename cond,
96 :     trueBranch = !trueBranch,
97 :     falseBranch = !falseBranch
98 :     }
99 :     in
100 :     IL.Node.replaceInEdge {src = !pred, oldDst = nd, dst = newNd};
101 :     IL.Node.replaceOutEdge {oldSrc = nd, src = newNd, dst = !trueBranch};
102 :     IL.Node.replaceOutEdge {oldSrc = nd, src = newNd, dst = !falseBranch}
103 :     end
104 :     else ()
105 :     | IL.ASSIGN{stm=(y, rhs), succ, ...} =>
106 :     if changed y
107 :     then IL.CFG.deleteNode nd (* deleting redundant assignment *)
108 :     else if (List.exists changed (IL.RHS.vars rhs))
109 :     (* rewrite node to rename variables *)
110 :     then IL.CFG.replaceNode(nd, IL.Node.mkASSIGN(y, IL.RHS.map rename rhs))
111 :     else ()
112 : jhr 1623 | IL.MASSIGN{stm=([], rator, xs), succ, ...} =>
113 :     if (List.exists changed xs)
114 :     (* rewrite node to rename variables *)
115 :     then IL.CFG.replaceNode(nd, IL.Node.mkMASSIGN([], rator, List.map rename xs))
116 :     else ()
117 : jhr 1556 | IL.MASSIGN{stm=(ys, rator, xs), succ, ...} =>
118 :     if List.all changed ys
119 :     then IL.CFG.deleteNode nd (* deleting redundant assignment *)
120 :     else if (List.exists changed xs)
121 :     (* rewrite node to rename variables *)
122 :     then IL.CFG.replaceNode(nd, IL.Node.mkMASSIGN(ys, rator, List.map rename xs))
123 :     else ()
124 : jhr 1148 | IL.NEW{strand, args, ...} =>
125 :     if List.exists changed args
126 :     then IL.CFG.replaceNode(nd, IL.Node.mkNEW{
127 :     strand=strand, args=List.map rename args
128 :     })
129 :     else ()
130 : jhr 1504 | IL.SAVE{lhs, rhs, ...} =>
131 :     if changed rhs
132 :     then IL.CFG.replaceNode(nd, IL.Node.mkSAVE(lhs, rename rhs))
133 :     else ()
134 : jhr 1148 | IL.EXIT{kind, live, ...} =>
135 :     if List.exists changed live
136 :     then let
137 :     val newNd = IL.Node.mkEXIT(kind, List.map rename live)
138 :     in
139 :     if IL.Node.same(nd, !exitNd)
140 :     then exitNd := newNd
141 :     else ();
142 :     IL.CFG.replaceNode (nd, newNd)
143 :     end
144 :     else ()
145 :     | _ => ()
146 :     (* end case *))
147 :     val _ = List.app doNode (IL.CFG.sort cfg)
148 :     val cfg = IL.CFG{entry = IL.CFG.entry cfg, exit = !exitNd}
149 :     in
150 :     IL.CFG.apply clearNode cfg;
151 :     cfg
152 :     end
153 : jhr 740
154 : jhr 1153 fun transformCFG (liveIn, renameIn, cfg) = let
155 : jhr 1148 val tbl = E.new()
156 : jhr 1504 val mkSTATE = E.mkSTATE tbl
157 : jhr 1148 val mkVAR = E.mkVAR tbl
158 :     val mkLIT = E.mkLIT tbl
159 :     val mkOP = E.mkOP tbl
160 : jhr 1556 val mkMULTIOP = E.mkMULTIOP tbl
161 : jhr 1148 val mkAPPLY = E.mkAPPLY tbl
162 :     val mkCONS = E.mkCONS tbl
163 :     val mkPHI = E.mkPHI tbl
164 :     (* convert a list of variables to a list of expressions *)
165 :     fun varsToExp (env, xs) = List.map varToExp xs
166 : jhr 740 (* convert an SSA RHS into a hash-consed expression *)
167 :     fun mkExp (env, rhs) = (case rhs
168 : jhr 1504 of IL.STATE x => mkSTATE x
169 :     | IL.VAR x => varToExp x
170 : jhr 1148 | IL.LIT l => mkLIT l
171 :     | IL.OP(rator, args) => mkOP(rator, varsToExp(env, args))
172 :     | IL.APPLY(f, args) => mkAPPLY(f, varsToExp(env, args))
173 :     | IL.CONS(ty, args) => mkCONS(ty, varsToExp(env, args))
174 : jhr 740 (* end case *))
175 : jhr 1148 (* walk the dominator tree computing value numbers *)
176 : jhr 740 fun vn (env, nd) = let
177 :     val env = (case IL.Node.kind nd
178 : jhr 741 of IL.JOIN{succ, phis, ...} => let
179 : jhr 1148 fun doPhi ((y, xs), (env, phis)) = let
180 : jhr 741 val vn::vns = List.map getVN xs
181 :     in
182 :     if List.all (fn vn' => IL.Var.same(vn, vn')) vns
183 : jhr 1148 then ((* a meaningless phi node; map y to vn *)
184 : jhr 1157 (* DEBUG Log.msg(concat["** meaningless phi node: ", IL.phiToString (y, xs), "\n"]);*)
185 : jhr 1153 ST.tick cntMeaninglessPhi;
186 : jhr 1148 List.map decUse xs;
187 :     setVN(y, vn);
188 :     (env, phis))
189 : jhr 741 else let
190 : jhr 1148 val exp = mkPHI(varsToExp(env, xs))
191 : jhr 741 in
192 :     case expToVN(env, exp)
193 : jhr 1148 of SOME vn' => ((* a redundant phi node *)
194 : jhr 1157 (* DEBUG Log.msg(concat["** redundant phi node: ", IL.phiToString (y, xs), "\n"]);*)
195 : jhr 1153 ST.tick cntRedundantPhi;
196 : jhr 1148 List.map decUse xs;
197 :     setVN(y, vn');
198 :     (env, phis))
199 :     | NONE => let
200 :     val xs = List.map rename xs
201 :     in
202 :     (bindVarToExp(env, y, exp), (y, xs)::phis)
203 :     end
204 : jhr 741 (* end case *)
205 :     end
206 :     end
207 : jhr 1148 val (env, remainingPhis) = List.foldl doPhi (env, []) (!phis)
208 : jhr 741 in
209 : jhr 1148 phis := List.rev remainingPhis;
210 :     env
211 : jhr 741 end
212 : jhr 740 | IL.ASSIGN{stm=(y, rhs), succ, ...} => let
213 :     val exp = mkExp(env, rhs)
214 :     in
215 :     case expToVN(env, exp)
216 : jhr 1148 of SOME vn => ((* y is redundant, so map it to vn *)
217 : jhr 1556 (* DEBUG ** Log.msg(concat["** redundant multi-assignment: ", IL.assignToString (y, rhs),*)
218 : jhr 1546 (* DEBUG **"; VN[", IL.Var.toString y, "] = ", IL.Var.toString vn, "\n"]);*)
219 : jhr 1153 ST.tick cntRedundantAssign;
220 : jhr 1148 setVN(y, vn);
221 :     env)
222 : jhr 1556 | NONE => bindVarToExp(env, y, exp)
223 : jhr 740 (* end case *)
224 :     end
225 : jhr 1556 | IL.MASSIGN{stm=(ys, rator, xs), succ, ...} => let
226 :     val xs = varsToExp(env, xs)
227 :     fun mkExps (env, _, []) = env
228 :     | mkExps (env, i, y::ys) = let
229 :     val exp = mkMULTIOP(i, rator, xs)
230 :     in
231 :     case expToVN(env, exp)
232 :     of SOME vn => ((* y is redundant, so map it to vn *)
233 :     ST.tick cntRedundantAssign;
234 :     setVN(y, vn);
235 :     mkExps (env, i+1, ys))
236 :     | NONE => mkExps (bindVarToExp(env, y, exp), i+1, ys)
237 :     (* end case *)
238 :     end
239 :     in
240 :     mkExps (env, 0, ys)
241 :     end
242 : jhr 740 | _ => env
243 :     (* end case *))
244 :     in
245 :     List.app (fn nd => vn (env, nd)) (D.children nd)
246 :     end
247 : jhr 1148 (* define the initial environment by mapping the liveIn variables to themselves *)
248 :     val env = List.foldl (fn (x, env) => bindVarToExp(env, x, mkVAR x)) emptyEnv liveIn
249 : jhr 1153 (* set the VN of the incoming renamed variables accordingly *)
250 :     val _ = List.app setVN renameIn
251 : jhr 740 in
252 : jhr 1148 D.computeTree cfg;
253 :     (* compute value numbers over the dominance tree *)
254 :     vn (env, IL.CFG.entry cfg);
255 :     D.clear cfg;
256 :     (* delete and rewrite nodes as necessary *)
257 : jhr 1153 rewriteCFG cfg before
258 :     (List.app clearVar liveIn; List.app (clearVar o #1) renameIn)
259 : jhr 1148 end
260 : jhr 740
261 : jhr 1153 fun transformCFG' (liveIn, renameIn, cfg) = let
262 :     val origLiveOut = IL.CFG.liveAtExit cfg
263 :     val cfg = transformCFG (liveIn, renameIn, cfg)
264 :     val liveOut = IL.CFG.liveAtExit cfg
265 :     (* compute a mapping from the original liveOut variables to their new names *)
266 :     val rename = let
267 :     fun findDups (x, x', rename) =
268 :     if IL.Var.same(x, x')
269 :     then rename
270 :     else IL.Var.Map.insert(rename, x, x')
271 :     in
272 :     ListPair.foldl findDups IL.Var.Map.empty (origLiveOut, liveOut)
273 :     end
274 :     (* filter out duplicate names from the liveOut list *)
275 :     val foundDup = ref false
276 :     val liveOut' = let
277 :     fun f (x, ys) = if List.exists (fn y => IL.Var.same(x, y)) ys
278 :     then (foundDup := true; ys)
279 :     else x::ys
280 :     in
281 :     List.foldr f [] liveOut
282 :     end
283 :     (* if there were any duplicates, then rewrite the exit node *)
284 :     val cfg = if !foundDup
285 :     then IL.CFG.updateExit(cfg, fn _ => liveOut')
286 :     else cfg
287 :     in
288 :     {cfg = cfg, rename = IL.Var.Map.foldli (fn (x, y, l) => (x, y)::l) renameIn rename}
289 :     end
290 :    
291 : jhr 1148 fun transform prog = let
292 : jhr 1505 val IL.Program{props, globalInit, initially, strands} = prog
293 : jhr 1153 val {cfg=globalInit, rename} = transformCFG' ([], [], globalInit)
294 : jhr 1148 val globals = IL.CFG.liveAtExit globalInit
295 : jhr 1153 (* transform the strand initialization code *)
296 :     val initially = if List.null rename
297 :     then initially
298 :     else let
299 :     val IL.Initially{isArray, rangeInit, iters, create} = initially
300 :     (* first process the range initialization code *)
301 :     val {cfg=rangeInit, rename} = transformCFG' (globals, rename, rangeInit)
302 :     val live = IL.CFG.liveAtExit rangeInit @ globals
303 :     (* create a function for renaming variables *)
304 :     fun mkRenameFn rename = let
305 :     val vMap = List.foldl IL.Var.Map.insert' IL.Var.Map.empty rename
306 :     fun renameVar x = (case IL.Var.Map.find (vMap, x)
307 :     of NONE => x
308 :     | SOME x' => x'
309 :     (* end case *))
310 :     in
311 :     renameVar
312 :     end
313 :     (* rename the bounds of the iterators *)
314 :     val iters = let
315 :     val renameVar = mkRenameFn rename
316 :     in
317 :     List.map (fn (x, lo, hi) => (x, renameVar lo, renameVar hi)) iters
318 :     end
319 :     (* process the body *)
320 :     val (cfg, strand, args) = create
321 :     val {cfg, rename} = transformCFG' (live, rename, cfg)
322 :     val create = (cfg, strand, List.map (mkRenameFn rename) args)
323 :     in
324 :     IL.Initially{
325 :     isArray = isArray, rangeInit = rangeInit,
326 :     iters = iters, create= create
327 :     }
328 :     end
329 :     (* transform a strand *)
330 : jhr 1148 fun transformStrand (IL.Strand{name, params, state, stateInit, methods}) = let
331 :     val liveIn = params @ globals
332 : jhr 1153 val stateInit = transformCFG (liveIn, rename, stateInit)
333 : jhr 1148 (* FIXME: what if a state variable becomes redundant? *)
334 : jhr 1504 fun transformMeth (IL.Method{name, body}) = let
335 : jhr 1153 val body = transformCFG (liveIn, rename, body)
336 : jhr 1148 in
337 : jhr 1504 IL.Method{name=name, body=body}
338 : jhr 1148 end
339 :     in
340 :     IL.Strand{
341 :     name = name,
342 :     params = params,
343 :     state = state,
344 :     stateInit = stateInit,
345 :     methods = List.map transformMeth methods
346 :     }
347 :     end
348 :     val strands = List.map transformStrand strands
349 :     in
350 :     IL.Program{
351 : jhr 1505 props = props,
352 : jhr 1148 globalInit = globalInit,
353 :     initially = initially,
354 :     strands = strands
355 :     }
356 :     end
357 :    
358 : jhr 740 end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0