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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2356 - (view) (download)

1 : jhr 2356 (* rewrite-fn.sml
2 :     *
3 :     * COPYRIGHT (c) 2013 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Infrastructure for rewriting the CFG.
7 :     *)
8 :    
9 :     signature REWRITE = sig
10 :     structure IL : SSA
11 :     (* rewrite the right-hand-side of an assignment; returns NONE if there are no changes
12 :     * and SOME assigns if the assignment is rewritten to the assigns list.
13 :     *)
14 :     val doAssign : IL.assign -> (IL.var * IL.rhs) list option
15 :     (* rewrite the rhs of an multi-assignment *)
16 :     val doMAssign : IL.massign -> (IL.var * IL.rhs) list option
17 :     (* eliminates unused variables; returns true if any changes *)
18 :     val elimUnusedVars : IL.cfg -> bool
19 :     end
20 :    
21 :     functor RewriteFn (R : REWRITE) : sig
22 :    
23 :     val transform : R.IL.program -> R.IL.program
24 :    
25 :     end = struct
26 :    
27 :     open R
28 :    
29 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
30 :    
31 :     (* simplify assignment and multi-assignment statements *)
32 :     fun simplify nd = let
33 :     fun rewrite (SOME[]) = (IL.CFG.deleteNode nd; true)
34 :     | rewrite (SOME assigns) = let
35 :     val assigns = List.map
36 :     (fn (y, rhs) => (IL.Var.setBinding(y, IL.VB_RHS rhs); IL.ASSGN(y, rhs)))
37 :     assigns
38 :     in
39 :     IL.CFG.replaceNodeWithCFG (nd, IL.CFG.mkBlock assigns);
40 :     true
41 :     end
42 :     | rewrite NONE = false
43 :     in
44 :     case IL.Node.kind nd
45 :     of IL.ASSIGN{stm=(y, rhs), ...} => if (useCount y = 0)
46 :     then false (* skip unused assignments *)
47 :     else rewrite (doAssign (y, rhs))
48 :     | IL.MASSIGN{stm, ...} => rewrite (doMAssign stm)
49 :     | _ => false
50 :     (* end case *)
51 :     end
52 :    
53 :     fun loopToFixPt f = let
54 :     fun loop anyChanges = if f() then loop true else anyChanges
55 :     in
56 :     loop false
57 :     end
58 :    
59 :     fun transform (prog as IL.Program{props, globalInit, initially, strands}) = let
60 :     fun simplifyCFG cfg = let
61 :     val changed = ref false
62 :     fun simplify' nd = if simplify nd then changed := true else ()
63 :     in
64 :     IL.CFG.apply simplify' cfg;
65 :     !changed
66 :     end
67 :     fun doCFG cfg = let
68 :     val changes = loopToFixPt (fn () => simplifyCFG cfg)
69 :     val changes = loopToFixPt (fn () => elimUnusedVars cfg) orelse changes
70 :     in
71 :     changes
72 :     end
73 :     fun doMethod (IL.Method{body, ...}) = doCFG body
74 :     fun doStrand (IL.Strand{stateInit, methods, ...}) = let
75 :     val changes = doCFG stateInit
76 :     val changes = List.foldl (fn (m, flg) => doMethod m orelse flg) changes methods
77 :     in
78 :     changes
79 :     end
80 :     fun optPass () = let
81 :     val changes = doCFG globalInit
82 :     val changes = List.foldl (fn (s, flg) => doStrand s orelse flg) changes strands
83 :     in
84 :     changes
85 :     end
86 :     in
87 :     loopToFixPt optPass;
88 :     (* FIXME: after optimization, we should filter out any globals that are now unused *)
89 :     IL.Program{
90 :     props = props,
91 :     globalInit = globalInit,
92 :     initially = initially, (* FIXME: we should optimize this code *)
93 :     strands = strands
94 :     }
95 :     end
96 :    
97 :     end

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