3 |
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu) |
* COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu) |
4 |
* All rights reserved. |
* All rights reserved. |
5 |
* |
* |
6 |
* Compute use counts for IL variables. |
* Compute use counts for IL variables and initialize their bindings. |
7 |
*) |
*) |
8 |
|
|
9 |
functor CensusFn (IL : SSA) : sig |
functor CensusFn (IL : SSA) : sig |
19 |
structure IL = IL |
structure IL = IL |
20 |
|
|
21 |
fun inc (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1) |
fun inc (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1) |
22 |
|
fun setBinding (IL.V{bind, ...}, vb) = bind := vb |
23 |
|
|
24 |
fun init (IL.Program{globals, globalInit, actors}) = let |
fun init (IL.Program{globals, globalInit, actors}) = let |
25 |
fun clearVar (IL.V{useCnt, ...}) = useCnt := 0 |
fun clearVar (IL.V{useCnt, ...}) = useCnt := 0 |
44 |
val incList = List.app inc |
val incList = List.app inc |
45 |
(* increment the counts of the variables used in a node *) |
(* increment the counts of the variables used in a node *) |
46 |
fun incNode (IL.ND{kind, ...}) = (case kind |
fun incNode (IL.ND{kind, ...}) = (case kind |
47 |
of IL.JOIN{phis, ...} => List.app (fn (_, xs) => incList xs) (!phis) |
of IL.JOIN{phis, ...} => let |
48 |
|
fun f (y, xs) = ( |
49 |
|
setBinding (y, IL.VB_PHI xs); |
50 |
|
incList xs) |
51 |
|
in |
52 |
|
List.app f (!phis) |
53 |
|
end |
54 |
| IL.COND{cond, ...} => inc cond |
| IL.COND{cond, ...} => inc cond |
55 |
| IL.BLOCK{body, ...} => let |
| IL.BLOCK{body, ...} => let |
56 |
fun incRHS (IL.VAR x) = inc x |
fun incRHS (y, rhs) = ( |
57 |
| incRHS (IL.LIT _) = () |
setBinding (y, IL.VB_RHS rhs); |
58 |
| incRHS (IL.OP(_, args)) = incList args |
case rhs |
59 |
| incRHS (IL.CONS args) = incList args |
of (IL.VAR x) => inc x |
60 |
|
| (IL.LIT _) => () |
61 |
|
| (IL.OP(_, args)) => incList args |
62 |
|
| (IL.CONS args) => incList args |
63 |
|
(* end case *)) |
64 |
in |
in |
65 |
List.app (fn (_, rhs) => incRHS rhs) (!body) |
List.app incRHS (!body) |
66 |
end |
end |
67 |
| IL.NEW{args, ...} => incList args |
| IL.NEW{args, ...} => incList args |
68 |
| _ => () |
| _ => () |
69 |
(* end case *)) |
(* end case *)) |
70 |
(* increment the counts of the variables used in an actor *) |
(* increment the counts of the variables used in an actor *) |
71 |
fun incActor (IL.Actor{params, state, stateInit, methods, ...}) = let |
fun incActor (IL.Actor{params, state, stateInit, methods, ...}) = let |
72 |
fun incMethod (IL.Method{stateOut, body, ...}) = ( |
fun incMethod (IL.Method{stateIn, stateOut, body, ...}) = ( |
73 |
|
List.app (fn x => setBinding(x, IL.VB_STATE_VAR)) stateIn; |
74 |
incList stateOut; |
incList stateOut; |
75 |
IL.applyToNodes incNode body) |
IL.applyToNodes incNode body) |
76 |
in |
in |
77 |
|
List.app (fn x => setBinding(x, IL.VB_PARAM)) params; |
78 |
IL.applyToNodes incNode stateInit; |
IL.applyToNodes incNode stateInit; |
79 |
List.app incMethod methods |
List.app incMethod methods |
80 |
end |
end |