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

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

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