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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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