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

SCM Repository

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

Annotation of /trunk/src/compiler/IL/census-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1232 - (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 368 fun setBinding (IL.V{bind, ...}, vb) = bind := vb
25 : jhr 338
26 : jhr 1116 fun init (IL.Program{globalInit, initially, strands}) = let
27 : jhr 341 fun clearVar (IL.V{useCnt, ...}) = useCnt := 0
28 :     (* clear the counts of the variables defined in a node *)
29 :     fun clearNode (IL.ND{kind, ...}) = (case kind
30 :     of IL.JOIN{phis, ...} => List.app (fn (x, _) => clearVar x) (!phis)
31 : jhr 1116 | IL.ASSIGN{stm=(x, _), ...} => clearVar x
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 341 fun clearMethod (IL.Method{stateIn, body, ...}) = (
45 :     List.app clearVar stateIn;
46 : jhr 1116 IL.CFG.apply clearNode body)
47 : jhr 341 in
48 :     List.app clearVar params;
49 : jhr 1116 List.app (clearVar o #2) state;
50 :     IL.CFG.apply clearNode stateInit;
51 : jhr 341 List.app clearMethod methods
52 :     end
53 :     (* increment the use counts of a list of variables *)
54 :     val incList = List.app inc
55 : jhr 1232 (* increment the counts of the variables used in a node. The exitCount function
56 :     * is used to count the live variables at exits, since the context affects the
57 :     * treatment of these.
58 :     *)
59 :     fun incNode exitCount (IL.ND{kind, ...}) = (case kind
60 : jhr 368 of IL.JOIN{phis, ...} => let
61 :     fun f (y, xs) = (
62 :     setBinding (y, IL.VB_PHI xs);
63 :     incList xs)
64 :     in
65 :     List.app f (!phis)
66 :     end
67 : jhr 341 | IL.COND{cond, ...} => inc cond
68 : jhr 1116 | IL.ASSIGN{stm = (y, rhs), ...} => (
69 :     setBinding (y, IL.VB_RHS rhs);
70 :     case rhs
71 :     of (IL.VAR x) => inc x
72 :     | (IL.LIT _) => ()
73 :     | (IL.OP(_, args)) => incList args
74 :     | (IL.APPLY(_, args)) => incList args
75 :     | (IL.CONS(_, args)) => incList args
76 :     (* end case *))
77 : jhr 341 | IL.NEW{args, ...} => incList args
78 : jhr 1232 | IL.EXIT{live, ...} => exitCount live
79 : jhr 341 | _ => ()
80 :     (* end case *))
81 : jhr 1116 (* increment the counts of variables used in the initially code *)
82 :     fun incInitially (IL.Initially{create, rangeInit, iters, ...}) = let
83 :     fun incIter (param, lo, hi) = (
84 :     setBinding (param, IL.VB_PARAM); (* QUESTION: should there be a special kind for this? *)
85 :     inc lo; inc hi)
86 :     in
87 : jhr 1232 IL.CFG.apply (incNode (fn live => incList live)) rangeInit;
88 : jhr 1116 List.app incIter iters;
89 : jhr 1232 IL.CFG.apply (incNode (fn _ => ())) (#1 create);
90 : jhr 1116 List.app inc (#3 create)
91 :     end
92 : jhr 511 (* increment the counts of the variables used in a strand *)
93 :     fun incStrand (IL.Strand{params, state, stateInit, methods, ...}) = let
94 : jhr 1116 fun incMethod (IL.Method{stateIn, body, ...}) = (
95 :     ListPair.app
96 :     (fn (x, (_, x')) => IL.Var.setBinding(x, IL.VB_RHS(IL.VAR x')))
97 :     (stateIn, state);
98 : jhr 1232 IL.CFG.apply (incNode (fn live => incList live)) body)
99 : jhr 341 in
100 : jhr 368 List.app (fn x => setBinding(x, IL.VB_PARAM)) params;
101 : jhr 1232 IL.CFG.apply (incNode (fn live => incList live)) stateInit;
102 : jhr 341 List.app incMethod methods
103 :     end
104 :     in
105 :     (* first clear the counts of all variables *)
106 : jhr 1116 IL.CFG.apply clearNode globalInit;
107 :     clearInitially initially;
108 : jhr 511 List.app clearStrand strands;
109 : jhr 341 (* then count uses *)
110 : jhr 1232 IL.CFG.apply (incNode (fn _ => ())) globalInit;
111 : jhr 1116 incInitially initially;
112 : jhr 511 List.app incStrand strands
113 : jhr 341 end
114 :    
115 : jhr 338 end

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