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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2497 - (view) (download)

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

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