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

SCM Repository

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

Annotation of /trunk/src/compiler/IL/forward-dfa-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3349 - (view) (download)

1 : jhr 257 (* forward-dfa-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 257 * All rights reserved.
7 :     *
8 :     * Forward data-flow analysis for IL code.
9 :     *)
10 :    
11 :     functor ForwardDFAFn (D : DOMAIN) :> sig
12 :    
13 : jhr 417 structure D : DOMAIN = D
14 : jhr 257
15 : jhr 414 (* given the entry value and root statement, do the forward DFA on the CFG and
16 : jhr 1116 * return the list of nodes that were analysed.
17 : jhr 414 *)
18 : jhr 1116 val analyse : D.t * D.IL.cfg -> D.IL.node list
19 : jhr 257
20 : jhr 414 (* get results for a node *)
21 :     val inValue : D.IL.node -> D.t
22 :     val outValue : D.IL.node -> D.t
23 : jhr 257
24 : jhr 258 (* scrub results to reclaim space *)
25 : jhr 414 val scrub : D.IL.node list -> unit
26 : jhr 257
27 :     end = struct
28 :    
29 :     structure D = D
30 :     structure IL = D.IL
31 :    
32 :     type result = IL.node list
33 :    
34 : jhr 414 val {setFn=setIn, getFn=getIn, clrFn= clrIn, ...} =
35 : jhr 257 PropList.newProp (fn (IL.ND{props, ...}) => props, fn _ => D.bottom)
36 : jhr 414 val {setFn=setOut, getFn=getOut, clrFn=clrOut, ...} =
37 :     PropList.newProp (fn (IL.ND{props, ...}) => props, fn _ => D.bottom)
38 : jhr 257
39 : jhr 414 fun clear node = (clrIn node; clrOut node)
40 :    
41 : jhr 1116 fun analyse (entryVal, cfg) = let
42 :     (* use DFS order to get quicker convergence *)
43 :     val nodes as entry::rest = IL.CFG.sort cfg
44 :     (* the first pass does not do change propagation, since we haven't computed anything yet *)
45 :     fun initNode nd = let
46 :     val inValue = D.join (List.map getOut (IL.Node.preds nd))
47 :     val outValue = D.transfer (inValue, nd)
48 :     in
49 :     setIn (nd, inValue);
50 :     setOut (nd, outValue)
51 :     end
52 :     (* once nodes are initialized, we iterate until we reach a fixed point *)
53 : jhr 257 fun iterate () = let
54 :     val anyChange = ref false
55 :     fun doNode nd = let
56 : jhr 414 val inValue = D.join (List.map getOut (IL.Node.preds nd))
57 : jhr 257 in
58 : jhr 414 if D.same(getIn nd, inValue)
59 :     then () (* input unchanged, so output will be unchanged *)
60 :     else let
61 :     val outValue = D.transfer (inValue, nd)
62 :     in
63 :     anyChange := true;
64 :     setIn (nd, inValue);
65 :     if D.same(getOut nd, outValue)
66 : jhr 417 then ()
67 :     else setOut(nd, outValue)
68 : jhr 414 end
69 : jhr 257 end
70 :     in
71 : jhr 1116 List.app doNode rest;
72 : jhr 257 if !anyChange then iterate() else ()
73 :     end
74 :     in
75 : jhr 1116 (* initialize the output of the CFG entry node *)
76 :     setOut (entry, entryVal);
77 :     (* initialize the rest of the nodes *)
78 :     List.app initNode rest;
79 :     (* iterate to a fixed point *)
80 :     iterate ();
81 :     nodes
82 : jhr 257 end
83 :    
84 : jhr 414 fun inValue nd = getIn nd
85 :     fun outValue nd = getOut nd
86 : jhr 257
87 : jhr 414 fun scrub nodes = List.app clear nodes
88 : jhr 257
89 :     end

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