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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/cfg-ir/rewrite-fn.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/cfg-ir/rewrite-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3937 - (view) (download)

1 : jhr 3517 (* rewrite-fn.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *
8 :     * Infrastructure for rewriting the CFG.
9 :     *)
10 :    
11 :     signature REWRITE = sig
12 :     structure IR : 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 : IR.assign -> (IR.var * IR.rhs) list option
17 :     (* rewrite the rhs of an multi-assignment *)
18 :     val doMAssign : IR.massign -> (IR.var * IR.rhs) list option
19 :     (* eliminates unused variables; returns true if any changes *)
20 :     val elimUnusedVars : IR.cfg -> bool
21 :     end
22 :    
23 :     functor RewriteFn (R : REWRITE) : sig
24 :    
25 :     val transform : R.IR.program -> R.IR.program
26 :    
27 :     end = struct
28 :    
29 :     open R
30 :    
31 :     fun useCount (IR.V{useCnt, ...}) = !useCnt
32 :    
33 :     (* simplify assignment and multi-assignment statements *)
34 :     fun simplify nd = let
35 :     fun rewrite (SOME[]) = (IR.CFG.deleteNode nd; true)
36 :     | rewrite (SOME assigns) = let
37 :     val assigns = List.map
38 :     (fn (y, rhs) => (IR.Var.setBinding(y, IR.VB_RHS rhs); IR.ASSGN(y, rhs)))
39 :     assigns
40 :     in
41 :     IR.CFG.replaceNodeWithCFG (nd, IR.CFG.mkBlock assigns);
42 :     true
43 :     end
44 :     | rewrite NONE = false
45 :     in
46 :     case IR.Node.kind nd
47 :     of IR.ASSIGN{stm=(y, rhs), ...} => if (useCount y = 0)
48 :     then false (* skip unused assignments *)
49 :     else rewrite (doAssign (y, rhs))
50 :     | IR.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 = let
62 :     val IR.Program{
63 :     props, consts, inputs, constInit, globals, globalInit, strand, create, update
64 :     } = prog
65 :     fun simplifyCFG cfg = let
66 :     val changed = ref false
67 :     fun simplify' nd = if simplify nd then changed := true else ()
68 :     in
69 :     IR.CFG.apply simplify' cfg;
70 :     !changed
71 :     end
72 :     fun doCFG cfg = let
73 :     val changes = loopToFixPt (fn () => simplifyCFG cfg)
74 :     val changes = loopToFixPt (fn () => elimUnusedVars cfg) orelse changes
75 :     in
76 :     changes
77 :     end
78 :     fun doOptionalCFG (NONE, changes) = changes
79 :     | doOptionalCFG (SOME cfg, changes) = doCFG cfg orelse changes
80 :     fun doCreate (IR.Create{code, ...}) =
81 :     loopToFixPt (fn () => elimUnusedVars code)
82 :     fun doStrand (IR.Strand{stateInit, initM, updateM, stabilizeM, ...}) = let
83 :     val changes = doCFG stateInit
84 :     val changes = doOptionalCFG (initM, changes)
85 :     val changes = doCFG updateM orelse changes
86 :     val changes = doOptionalCFG (stabilizeM, changes)
87 :     in
88 :     changes
89 :     end
90 :     fun optPass () = let
91 :     val changes = doCFG constInit
92 :     val changes = doCFG globalInit
93 :     val changes = doCreate create orelse changes
94 :     val changes = doStrand strand orelse changes
95 :     val changes = doOptionalCFG (update, changes)
96 :     in
97 :     changes
98 :     end
99 : jhr 3937 (* filter out unused globals and update the program properties if necessary. Note that
100 :     * we never remove unused inputs, since that would affect the command-line interface.
101 :     *)
102 :     (* FIXME: what about consts? *)
103 : jhr 3517 val (props, globals) = (case (globals, List.filter (fn x => IR.GlobalVar.useCount x > 0) globals)
104 :     of ([], _) => (props, globals)
105 :     | (_, []) => (Properties.clearProp Properties.HasGlobals props, [])
106 :     | (_, liveGlobals) => (props, liveGlobals)
107 :     (* end case *))
108 :     in
109 :     loopToFixPt optPass;
110 :     IR.Program{
111 :     props = props,
112 :     consts = consts,
113 :     inputs = inputs,
114 :     constInit = constInit,
115 :     globals = globals,
116 :     globalInit = globalInit,
117 :     create = create,
118 :     strand = strand,
119 :     update = update
120 :     }
121 :     end
122 :    
123 :     end

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