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

1 : jhr 338 (* census-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 338 * All rights reserved.
7 :     *
8 : jhr 368 * Compute use counts for IL variables and initialize their bindings.
9 : jhr 338 *)
10 :    
11 :     functor CensusFn (IL : SSA) : sig
12 :    
13 :     structure IL : SSA
14 :    
15 :     val init : IL.program -> unit
16 :    
17 : jhr 341 val inc : IL.var -> unit
18 : jhr 1116 val dec : IL.var -> unit
19 : jhr 341
20 : jhr 338 end = struct
21 :    
22 :     structure IL = IL
23 :    
24 : jhr 341 fun inc (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
25 : jhr 1116 fun dec (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
26 : jhr 338
27 : jhr 1640 fun init (IL.Program{globalInit, initially, strands, ...}) = let
28 : jhr 341 fun clearVar (IL.V{useCnt, ...}) = useCnt := 0
29 :     (* clear the counts of the variables defined in a node *)
30 :     fun clearNode (IL.ND{kind, ...}) = (case kind
31 :     of IL.JOIN{phis, ...} => List.app (fn (x, _) => clearVar x) (!phis)
32 : jhr 1116 | IL.ASSIGN{stm=(x, _), ...} => clearVar x
33 : jhr 1640 | IL.MASSIGN{stm=(xs, _, _), ...} => List.app clearVar xs
34 : jhr 341 | _ => ()
35 :     (* end case *))
36 : jhr 1116 (* clear the counts of the initially code *)
37 :     fun clearInitially (IL.Initially{rangeInit, iters, create, ...}) = let
38 :     fun clearIter (param, lo, hi) = clearVar param
39 :     in
40 :     IL.CFG.apply clearNode rangeInit;
41 :     List.app clearIter iters;
42 :     IL.CFG.apply clearNode (#1 create)
43 :     end
44 : jhr 511 (* clear the counts of the variables defined in an strand *)
45 :     fun clearStrand (IL.Strand{params, state, stateInit, methods, ...}) = let
46 : jhr 1640 fun clearMethod (IL.Method{body, ...}) = IL.CFG.apply clearNode body
47 : jhr 341 in
48 :     List.app clearVar params;
49 : jhr 1116 IL.CFG.apply clearNode stateInit;
50 : jhr 341 List.app clearMethod methods
51 :     end
52 :     (* increment the use counts of a list of variables *)
53 :     val incList = List.app inc
54 : jhr 1232 (* increment the counts of the variables used in a node. The exitCount function
55 :     * is used to count the live variables at exits, since the context affects the
56 :     * treatment of these.
57 :     *)
58 : jhr 2636 (* FIXME: setting the binding here is probably redundant, since the mk functions do it too! *)
59 : jhr 1232 fun incNode exitCount (IL.ND{kind, ...}) = (case kind
60 : jhr 368 of IL.JOIN{phis, ...} => let
61 : jhr 2356 fun f (y, xs) = incList xs
62 : jhr 368 in
63 :     List.app f (!phis)
64 :     end
65 : jhr 341 | IL.COND{cond, ...} => inc cond
66 : jhr 2356 | IL.ASSIGN{stm = (y, rhs), ...} => IL.RHS.app inc rhs
67 :     | IL.MASSIGN{stm = (ys, rator, xs), ...} => List.app inc xs
68 : jhr 341 | IL.NEW{args, ...} => incList args
69 : jhr 1640 | IL.SAVE{rhs, ...} => inc rhs
70 : jhr 1232 | IL.EXIT{live, ...} => exitCount live
71 : jhr 341 | _ => ()
72 :     (* end case *))
73 : jhr 1116 (* increment the counts of variables used in the initially code *)
74 :     fun incInitially (IL.Initially{create, rangeInit, iters, ...}) = let
75 : jhr 2356 fun incIter (param, lo, hi) = (inc lo; inc hi)
76 : jhr 1116 in
77 : jhr 1232 IL.CFG.apply (incNode (fn live => incList live)) rangeInit;
78 : jhr 1116 List.app incIter iters;
79 : jhr 1232 IL.CFG.apply (incNode (fn _ => ())) (#1 create);
80 : jhr 1116 List.app inc (#3 create)
81 :     end
82 : jhr 511 (* increment the counts of the variables used in a strand *)
83 :     fun incStrand (IL.Strand{params, state, stateInit, methods, ...}) = let
84 : jhr 1640 fun incMethod (IL.Method{body, ...}) =
85 :     IL.CFG.apply (incNode (fn live => incList live)) body
86 : jhr 341 in
87 : jhr 1232 IL.CFG.apply (incNode (fn live => incList live)) stateInit;
88 : jhr 341 List.app incMethod methods
89 :     end
90 :     in
91 :     (* first clear the counts of all variables *)
92 : jhr 1116 IL.CFG.apply clearNode globalInit;
93 :     clearInitially initially;
94 : jhr 511 List.app clearStrand strands;
95 : jhr 341 (* then count uses *)
96 : jhr 1232 IL.CFG.apply (incNode (fn _ => ())) globalInit;
97 : jhr 1116 incInitially initially;
98 : jhr 511 List.app incStrand strands
99 : jhr 341 end
100 :    
101 : jhr 338 end

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