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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1786 - (view) (download)
Original Path: branches/vis12/src/compiler/IL/value-numbering-fn.sml

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

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