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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/high-opt/normalize.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/high-opt/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3517 - (view) (download)

1 : jhr 3513 (* normalize.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 :    
9 :     structure Normalize : sig
10 :    
11 :     val transform : HighIR.program -> HighIR.program
12 :    
13 :     end = struct
14 :    
15 :     structure IR = HighIR
16 :     structure Op = HighOps
17 :     structure V = IR.Var
18 : jhr 3517 structure ST = Stats
19 : jhr 3513
20 : jhr 3517 (********** Counters for statistics **********)
21 :     val cntUnused = ST.newCounter "high-opt:unused"
22 :    
23 : jhr 3513 structure UnusedElim = UnusedElimFn (
24 :     structure IR = IR
25 :     val cntUnused = cntUnused)
26 :    
27 :     fun useCount (IR.V{useCnt, ...}) = !useCnt
28 :    
29 :     (* adjust a variable's use count *)
30 :     fun incUse (IR.V{useCnt, ...}) = (useCnt := !useCnt + 1)
31 :     fun decUse (IR.V{useCnt, ...}) = (useCnt := !useCnt - 1)
32 :     fun use x = (incUse x; x)
33 :    
34 :     fun isEin x = (case V.binding x
35 :     of IR.VB_RHS(rhs as IR.EINAPP(e, arg)) => SOME rhs
36 :     | IR.VB_RHS(IR.VAR x') => isEin x'
37 :     | _ => NONE
38 :     (* end case *))
39 :    
40 :     (* doNormalize : EIN->EIN
41 :     * Orders EIN, normalizes it, then cleans the summation
42 :     *)
43 : jhr 3517 fun doNormalize e = let
44 : jhr 3515 val ordered = Reorder.transform e
45 : jhr 3517 val e = NormalizeEin.transform ordered
46 : jhr 3513 in
47 : jhr 3515 SummationEin.cleanSummation e
48 : jhr 3513 end
49 :    
50 :     fun nameCnt e = String.concat[V.toString e, "(", Int.toString(useCount e), ")"]
51 :     fun nameCnts e = String.concatWithMap "" nameCnt e
52 :    
53 :     (* rewriteEin : EIN.Params*int*int*EIN*HighIR Vars* rhs list* EIN *HighIR Var
54 :     * -> int*EIN*int*rhs list
55 :     * Orders EIN, normalizes it, then cleans the summation orig-original EIN
56 :     *)
57 :     fun rewriteEin (paramCount, place, changed, newE, newArgs, done, newEinApp, orig, lhs)= (
58 :     case List.nth(paramCount, place)
59 : jhr 3517 of Ein.TEN(0, _) => (changed, orig, place+1, done@[newEinApp])
60 : jhr 3513 | _ => let
61 :     val rtnArgs = done @ newArgs
62 :     val (c, subst) = App.app(orig, place, newE, newArgs, done)
63 :     in
64 :     if (c = 0)
65 : jhr 3517 then (true, subst, place + length newArgs, rtnArgs)
66 : jhr 3513 else (
67 : jhr 3517 incUse lhs; List.app incUse newArgs; decUse newEinApp;
68 : jhr 3513 (true, subst, place + length newArgs, rtnArgs))
69 :     end
70 :     (* end case *))
71 :    
72 :     (* doRHS: HighIR.var * rhs -> (var * rhs) list option
73 :     * Looks at each argument to the original EINAPP.
74 :     * If it is another EIN APP calls foundEIN to do application
75 :     * "place"-The Param-id for the EIN operator.
76 :     * Keeps track of the place of the argument in substitution.
77 :     *)
78 :     fun doRHS (lhs, IR.EINAPP(ein, args)) = let
79 :     fun rewrite (false, _, _, [], _) = NONE
80 :     | rewrite (true, orig, _, [], args') =
81 : jhr 3517 SOME[(lhs, IR.EINAPP(doNormalize orig, args'))]
82 : jhr 3513 | rewrite (changed, orig, place, e::es, args') = (case isEin e
83 :     of NONE => rewrite(changed, orig, place+1, es, args'@[e])
84 :     | SOME(IR.EINAPP(newE, newA)) => let
85 :     val Ein.EIN{params, index, body} = orig
86 :     val (changed, e', place', done') =
87 :     rewriteEin (params, place, changed, newE, newA, args', e, orig, lhs)
88 :     in
89 :     rewrite(changed, e', place', es, done')
90 :     end
91 :     | _ => raise Fail"isEin did not work"
92 :     (* end case *))
93 :     in
94 :     rewrite (false, ein, 0, args, [])
95 :     end
96 :     | doRHS _ = NONE
97 :    
98 :     structure Rewrite = RewriteFn (
99 :     struct
100 :     structure IR = IR
101 :     val doAssign = doRHS
102 :    
103 :     fun doMAssign _ = NONE
104 :     val elimUnusedVars = UnusedElim.reduce
105 :     end)
106 :    
107 :     val transform = Rewrite.transform
108 :    
109 :     end

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