SCM Repository
Annotation of /branches/staging/src/compiler/IL/census-fn.sml
Parent Directory
|
Revision Log
Revision 2360 - (view) (download)
1 : | jhr | 338 | (* census-fn.sml |
2 : | * | ||
3 : | jhr | 435 | * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu) |
4 : | jhr | 338 | * All rights reserved. |
5 : | * | ||
6 : | jhr | 368 | * Compute use counts for IL variables and initialize their bindings. |
7 : | jhr | 338 | *) |
8 : | |||
9 : | functor CensusFn (IL : SSA) : sig | ||
10 : | |||
11 : | structure IL : SSA | ||
12 : | |||
13 : | val init : IL.program -> unit | ||
14 : | |||
15 : | jhr | 341 | val inc : IL.var -> unit |
16 : | jhr | 1116 | val dec : IL.var -> unit |
17 : | jhr | 341 | |
18 : | jhr | 338 | end = struct |
19 : | |||
20 : | structure IL = IL | ||
21 : | |||
22 : | jhr | 341 | fun inc (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1) |
23 : | jhr | 1116 | fun dec (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1) |
24 : | jhr | 338 | |
25 : | jhr | 1640 | fun init (IL.Program{globalInit, initially, strands, ...}) = let |
26 : | jhr | 341 | fun clearVar (IL.V{useCnt, ...}) = useCnt := 0 |
27 : | (* clear the counts of the variables defined in a node *) | ||
28 : | fun clearNode (IL.ND{kind, ...}) = (case kind | ||
29 : | of IL.JOIN{phis, ...} => List.app (fn (x, _) => clearVar x) (!phis) | ||
30 : | jhr | 1116 | | IL.ASSIGN{stm=(x, _), ...} => clearVar x |
31 : | jhr | 1640 | | IL.MASSIGN{stm=(xs, _, _), ...} => List.app clearVar xs |
32 : | jhr | 341 | | _ => () |
33 : | (* end case *)) | ||
34 : | jhr | 1116 | (* clear the counts of the initially code *) |
35 : | fun clearInitially (IL.Initially{rangeInit, iters, create, ...}) = let | ||
36 : | fun clearIter (param, lo, hi) = clearVar param | ||
37 : | in | ||
38 : | IL.CFG.apply clearNode rangeInit; | ||
39 : | List.app clearIter iters; | ||
40 : | IL.CFG.apply clearNode (#1 create) | ||
41 : | end | ||
42 : | jhr | 511 | (* clear the counts of the variables defined in an strand *) |
43 : | fun clearStrand (IL.Strand{params, state, stateInit, methods, ...}) = let | ||
44 : | jhr | 1640 | fun clearMethod (IL.Method{body, ...}) = IL.CFG.apply clearNode body |
45 : | jhr | 341 | in |
46 : | List.app clearVar params; | ||
47 : | jhr | 1116 | IL.CFG.apply clearNode stateInit; |
48 : | jhr | 341 | List.app clearMethod methods |
49 : | end | ||
50 : | (* increment the use counts of a list of variables *) | ||
51 : | val incList = List.app inc | ||
52 : | jhr | 1232 | (* increment the counts of the variables used in a node. The exitCount function |
53 : | * is used to count the live variables at exits, since the context affects the | ||
54 : | * treatment of these. | ||
55 : | *) | ||
56 : | fun incNode exitCount (IL.ND{kind, ...}) = (case kind | ||
57 : | jhr | 368 | of IL.JOIN{phis, ...} => let |
58 : | jhr | 1934 | fun f (y, xs) = incList xs |
59 : | jhr | 368 | in |
60 : | List.app f (!phis) | ||
61 : | end | ||
62 : | jhr | 341 | | IL.COND{cond, ...} => inc cond |
63 : | jhr | 1934 | | IL.ASSIGN{stm = (y, rhs), ...} => IL.RHS.app inc rhs |
64 : | | IL.MASSIGN{stm = (ys, rator, xs), ...} => List.app inc xs | ||
65 : | jhr | 341 | | IL.NEW{args, ...} => incList args |
66 : | jhr | 1640 | | IL.SAVE{rhs, ...} => inc rhs |
67 : | jhr | 1232 | | IL.EXIT{live, ...} => exitCount live |
68 : | jhr | 341 | | _ => () |
69 : | (* end case *)) | ||
70 : | jhr | 1116 | (* increment the counts of variables used in the initially code *) |
71 : | fun incInitially (IL.Initially{create, rangeInit, iters, ...}) = let | ||
72 : | jhr | 1934 | fun incIter (param, lo, hi) = (inc lo; inc hi) |
73 : | jhr | 1116 | in |
74 : | jhr | 1232 | IL.CFG.apply (incNode (fn live => incList live)) rangeInit; |
75 : | jhr | 1116 | List.app incIter iters; |
76 : | jhr | 1232 | IL.CFG.apply (incNode (fn _ => ())) (#1 create); |
77 : | jhr | 1116 | List.app inc (#3 create) |
78 : | end | ||
79 : | jhr | 511 | (* increment the counts of the variables used in a strand *) |
80 : | fun incStrand (IL.Strand{params, state, stateInit, methods, ...}) = let | ||
81 : | jhr | 1640 | fun incMethod (IL.Method{body, ...}) = |
82 : | IL.CFG.apply (incNode (fn live => incList live)) body | ||
83 : | jhr | 341 | in |
84 : | jhr | 1232 | IL.CFG.apply (incNode (fn live => incList live)) stateInit; |
85 : | jhr | 341 | List.app incMethod methods |
86 : | end | ||
87 : | in | ||
88 : | (* first clear the counts of all variables *) | ||
89 : | jhr | 1116 | IL.CFG.apply clearNode globalInit; |
90 : | clearInitially initially; | ||
91 : | jhr | 511 | List.app clearStrand strands; |
92 : | jhr | 341 | (* then count uses *) |
93 : | jhr | 1232 | IL.CFG.apply (incNode (fn _ => ())) globalInit; |
94 : | jhr | 1116 | incInitially initially; |
95 : | jhr | 511 | List.app incStrand strands |
96 : | jhr | 341 | end |
97 : | |||
98 : | jhr | 338 | end |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |