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 4733 - (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 : jhr 4317 val IR.Program{
63 :     props, consts, inputs, constInit, globals,
64 : jhr 4491 funcs, globInit, strand, create, start, update
65 : jhr 4317 } = prog
66 : jhr 3517 fun simplifyCFG cfg = let
67 :     val changed = ref false
68 :     fun simplify' nd = if simplify nd then changed := true else ()
69 :     in
70 :     IR.CFG.apply simplify' cfg;
71 :     !changed
72 :     end
73 :     fun doCFG cfg = let
74 :     val changes = loopToFixPt (fn () => simplifyCFG cfg)
75 :     val changes = loopToFixPt (fn () => elimUnusedVars cfg) orelse changes
76 :     in
77 :     changes
78 :     end
79 : jhr 4317 fun doOptionalCFG (NONE, changes) = changes
80 :     | doOptionalCFG (SOME cfg, changes) = doCFG cfg orelse changes
81 :     fun doFunc (IR.Func{body, ...}, changes) = doCFG body orelse changes
82 : jhr 4491 fun doStrand (IR.Strand{stateInit, startM, updateM, stabilizeM, ...}) = let
83 : jhr 3517 val changes = doCFG stateInit
84 : jhr 4491 val changes = doOptionalCFG (startM, changes)
85 : jhr 4317 val changes = doCFG updateM orelse changes
86 :     val changes = doOptionalCFG (stabilizeM, changes)
87 : jhr 3517 in
88 :     changes
89 :     end
90 : jhr 4733 fun doCreate (Create.Create{code, ...}) = doCFG code
91 : jhr 3517 fun optPass () = let
92 :     val changes = doCFG constInit
93 : jhr 4317 val changes = List.foldl doFunc changes funcs
94 : jhr 3995 val changes = doCFG globInit
95 : jhr 4163 val changes = doStrand strand orelse changes
96 : jhr 3517 val changes = doCreate create orelse changes
97 : jhr 4491 val changes = doOptionalCFG (start, changes)
98 : jhr 4317 val changes = doOptionalCFG (update, changes)
99 : jhr 3517 in
100 :     changes
101 :     end
102 : jhr 4317 (* run the rewrite passes to a fixed point *)
103 :     val _ = loopToFixPt optPass;
104 : jhr 4181 (* filter out unused globals. Note that we never remove unused inputs, since
105 : jhr 4317 * that would affect the command-line interface.
106 : jhr 3937 *)
107 : jhr 4181 val globals = List.filter (fn x => IR.GlobalVar.useCount x > 0) globals
108 : jhr 4317 (* update the program properties if necessary. *)
109 :     val props = if (null globals) andalso (null inputs)
110 :     then Properties.clearProp Properties.HasGlobals props
111 :     else props
112 : jhr 3517 in
113 :     IR.Program{
114 :     props = props,
115 : jhr 4317 consts = consts,
116 :     inputs = inputs,
117 :     constInit = constInit,
118 : jhr 3517 globals = globals,
119 : jhr 4317 funcs = funcs,
120 : jhr 3995 globInit = globInit,
121 : jhr 3517 create = create,
122 :     strand = strand,
123 : jhr 4491 start = start,
124 : jhr 4317 update = update
125 : jhr 3517 }
126 :     end
127 :    
128 :     end

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