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

SCM Repository

[diderot] Annotation of /branches/lamont/src/compiler/mid-il/mid-contract.sml
ViewVC logotype

Annotation of /branches/lamont/src/compiler/mid-il/mid-contract.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2081 - (view) (download)

1 : jhr 1640 (* mid-contract.sml
2 :     *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Contraction phase for MidIL.
7 :     *)
8 :    
9 :     structure MidContract : sig
10 :    
11 :     val transform : MidIL.program -> MidIL.program
12 :    
13 :     end = struct
14 :    
15 :     structure IL = MidIL
16 :     structure Op = MidOps
17 :     structure V = IL.Var
18 :     structure ST = Stats
19 :    
20 :     (********** Counters for statistics **********)
21 :     val cntEigenVals = ST.newCounter "mid-opt:eigen-vals"
22 :     val cntUnused = ST.newCounter "mid-opt:unused"
23 :     val firstCounter = cntEigenVals
24 :     val lastCounter = cntUnused
25 :    
26 :     structure UnusedElim = UnusedElimFn (
27 :     structure IL = IL
28 :     val cntUnused = cntUnused)
29 :    
30 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
31 :    
32 :     (* adjust a variable's use count *)
33 :     fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
34 :     fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
35 :    
36 :     (* simplify expressions *)
37 :     fun simplify nd = (case IL.Node.kind nd
38 :     of IL.MASSIGN{stm=(ys, rator, xs), ...} => (case (ys, rator)
39 :     of ([vals, vecs], Op.EigenVecs2x2) =>
40 :     if (useCount vecs = 0)
41 :     then let
42 :     val newNd = IL.Node.mkASSIGN(vals, IL.OP(Op.EigenVals2x2, xs))
43 :     in
44 :     ST.tick cntEigenVals;
45 :     IL.CFG.replaceNode (nd, newNd)
46 :     end
47 :     else ()
48 :     | ([vals, vecs], Op.EigenVecs3x3) =>
49 :     if (useCount vecs = 0)
50 :     then let
51 :     val newNd = IL.Node.mkASSIGN(vals, IL.OP(Op.EigenVals3x3, xs))
52 :     in
53 :     ST.tick cntEigenVals;
54 :     IL.CFG.replaceNode (nd, newNd)
55 :     end
56 :     else ()
57 :     | _ => ()
58 :     (* end case *))
59 :     | _ => ()
60 :     (* end case *))
61 :    
62 :     fun loopToFixPt f = let
63 :     fun loop n = let
64 :     val () = f ()
65 :     val n' = Stats.sum{from=firstCounter, to=lastCounter}
66 :     in
67 :     if (n = n') then () else loop n'
68 :     end
69 :     in
70 :     loop (Stats.sum{from=firstCounter, to=lastCounter})
71 :     end
72 :    
73 :     fun transform (prog as IL.Program{props, globalInit, initially, strands}) = let
74 :     fun doCFG cfg = (
75 :     loopToFixPt (fn () => IL.CFG.apply simplify cfg);
76 :     loopToFixPt (fn () => ignore(UnusedElim.reduce cfg)))
77 :     fun doMethod (IL.Method{body, ...}) = doCFG body
78 :     fun doStrand (IL.Strand{stateInit, methods, ...}) = (
79 :     doCFG stateInit;
80 :     List.app doMethod methods)
81 :     fun optPass () = (
82 :     doCFG globalInit;
83 :     List.app doStrand strands)
84 :     in
85 :     loopToFixPt optPass;
86 :     (* FIXME: after optimization, we should filter out any globals that are now unused *)
87 :     IL.Program{
88 :     props = props,
89 :     globalInit = globalInit,
90 :     initially = initially, (* FIXME: we should optimize this code *)
91 :     strands = strands
92 :     }
93 :     end
94 :    
95 :     end

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