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 3349 - (view) (download)

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

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