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

SCM Repository

[diderot] Annotation of /branches/vis12-cl/src/compiler/IL/backward-dfa-fn.sml
ViewVC logotype

Annotation of /branches/vis12-cl/src/compiler/IL/backward-dfa-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2402 - (view) (download)

1 : jhr 258 (* backward-dfa-fn.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 258 * All rights reserved.
5 :     *
6 :     * Backward data-flow analysis for IL code.
7 :     *)
8 :    
9 :     functor BackwardDFAFn (D : DOMAIN) :> sig
10 :    
11 :     structure D : DOMAIN
12 :    
13 : jhr 414 (* given a function for defining out values for exits and the root node, do the
14 :     * backward DFA on the CFG and return the list of nodes analysed.
15 : jhr 258 *)
16 : jhr 1116 val analyse : (D.IL.node -> D.t) * D.IL.cfg -> D.IL.node list
17 : jhr 258
18 : jhr 414 (* get results for a node *)
19 :     val inValue : D.IL.node -> D.t
20 :     val outValue : D.IL.node -> D.t
21 : jhr 258
22 :     (* scrub results to reclaim space *)
23 : jhr 414 val scrub : D.IL.node list -> unit
24 : jhr 258
25 :     end = struct
26 :    
27 :     structure D = D
28 :     structure IL = D.IL
29 :    
30 : jhr 414 val {setFn=setIn, getFn=getIn, clrFn= clrIn, ...} =
31 : jhr 258 PropList.newProp (fn (IL.ND{props, ...}) => props, fn _ => D.bottom)
32 : jhr 414 val {setFn=setOut, getFn=getOut, clrFn=clrOut, ...} =
33 :     PropList.newProp (fn (IL.ND{props, ...}) => props, fn _ => D.bottom)
34 : jhr 258
35 : jhr 414 fun clear node = (clrIn node; clrOut node)
36 :    
37 : jhr 1116 fun analyse (exitVal, cfg) = let
38 :     (* use reverse DFS order to get quicker convergence *)
39 :     val nodes = List.rev (IL.CFG.sort cfg)
40 :     (* the first pass does not do change propagation, since we haven't computed anything yet *)
41 :     fun initNode nd = if IL.Node.hasSucc nd
42 :     then let
43 :     val outValue = D.join (List.map getIn (IL.Node.succs nd))
44 :     val inValue = D.transfer (outValue, nd)
45 :     in
46 :     setOut (nd, outValue);
47 :     setIn (nd, inValue)
48 :     end
49 :     else setIn(nd, exitVal nd) (* exit node *)
50 :     (* once nodes are initialized, we iterate until we reach a fixed point *)
51 : jhr 258 fun iterate () = let
52 :     val anyChange = ref false
53 :     fun doNode nd = let
54 : jhr 414 val outValue = D.join (List.map getIn (IL.Node.succs nd))
55 : jhr 258 in
56 : jhr 414 if D.same(getOut nd, outValue)
57 : jhr 1116 then ()(* output unchanged, so input will be unchanged *)
58 : jhr 414 else let
59 :     val inValue = D.transfer (outValue, nd)
60 :     in
61 :     anyChange := true;
62 :     setOut (nd, outValue);
63 :     if D.same(getIn nd, inValue)
64 :     then ()
65 :     else setIn(nd, inValue)
66 :     end
67 : jhr 258 end
68 :     in
69 :     List.app doNode nodes;
70 :     if !anyChange then iterate() else ()
71 :     end
72 :     in
73 : jhr 1116 (* initialize the rest of the nodes *)
74 :     List.app initNode nodes;
75 :     (* iterate to a fixed point *)
76 :     iterate ();
77 :     nodes
78 : jhr 258 end
79 :    
80 : jhr 414 fun inValue nd = getIn nd
81 :     fun outValue nd = getOut nd
82 : jhr 258
83 : jhr 414 fun scrub nodes = List.app clear nodes
84 : jhr 258
85 :     end

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