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

SCM Repository

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

Annotation of /branches/charisee/src/compiler/high-il/normalize.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2845 - (view) (download)

1 : jhr 1232 (* normalize.sml
2 :     *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure Normalize : sig
8 :    
9 :     val transform : HighIL.program -> HighIL.program
10 :    
11 :     end = struct
12 :    
13 : cchiw 2507
14 : jhr 1232 structure IL = HighIL
15 :     structure Op = HighOps
16 :     structure V = IL.Var
17 : jhr 2356 structure Ty = HighILTypes
18 : jhr 1232 structure ST = Stats
19 : cchiw 2400 structure NE = NormalizeEin
20 : cchiw 2499 structure P=Printer
21 : cchiw 2515 structure Order=OrderEin
22 : cchiw 2605 structure mk=mkOperators
23 : cchiw 2507 structure App=App
24 : cchiw 2605 structure S=SummationEin
25 : jhr 1232
26 : cchiw 2507
27 : cchiw 2400 (********** Counters for statistics **********)
28 : jhr 2356 val cntInsideScale = ST.newCounter "high-opt:inside-scale"
29 :     val cntInsideOffset = ST.newCounter "high-opt:inside-offset"
30 :     val cntInsideNeg = ST.newCounter "high-opt:inside-meg"
31 :     val cntInsideCurl = ST.newCounter "high-opt:inside-curl"
32 :     val cntInsideDiff = ST.newCounter "high-opt:inside-diff"
33 :     val cntProbeAdd = ST.newCounter "high-opt:probe-add"
34 :     val cntProbeSub = ST.newCounter "high-opt:probe-sub"
35 :     val cntProbeScale = ST.newCounter "high-opt:probe-scale"
36 :     val cntProbeOffset = ST.newCounter "high-opt:probe-offset"
37 :     val cntProbeNeg = ST.newCounter "high-opt:probe-neg"
38 :     val cntProbeCurl = ST.newCounter "high-opt:probe-curl"
39 :     val cntDiffField = ST.newCounter "high-opt:diff-field"
40 :     val cntDiffAdd = ST.newCounter "high-opt:diff-add"
41 :     val cntDiffScale = ST.newCounter "high-opt:diff-scale"
42 :     val cntDiffOffset = ST.newCounter "high-opt:diff-offset"
43 :     val cntDiffNeg = ST.newCounter "high-opt:diff-neg"
44 :     val cntCurlScale = ST.newCounter "high-opt:curl-scale"
45 :     val cntCurlNeg = ST.newCounter "high-opt:curl-neg"
46 :     val cntUnused = ST.newCounter "high-opt:unused"
47 :     val firstCounter = cntInsideScale
48 : jhr 1232 val lastCounter = cntUnused
49 :    
50 :     structure UnusedElim = UnusedElimFn (
51 : jhr 2356 structure IL = IL
52 :     val cntUnused = cntUnused)
53 : jhr 1232
54 :     fun useCount (IL.V{useCnt, ...}) = !useCnt
55 : cchiw 2605 (* adjust a variable's use count *)
56 : jhr 1232 fun incUse (IL.V{useCnt, ...}) = (useCnt := !useCnt + 1)
57 :     fun decUse (IL.V{useCnt, ...}) = (useCnt := !useCnt - 1)
58 : jhr 2356 fun use x = (incUse x; x)
59 : cchiw 2605 fun isEin x = (case V.binding x
60 : cchiw 2508 of IL.VB_RHS(IL.EINAPP (e,arg))=>SOME(IL.EINAPP (e,arg))
61 : cchiw 2525 | IL.VB_RHS(IL.VAR x') => isEin x'
62 : cchiw 2507 |_=>NONE
63 :     (* end case *))
64 :    
65 : cchiw 2844 val testing=0
66 :     fun testp e=(case testing
67 :     of 1 => (print(String.concat e);1)
68 :     | _ => 1
69 :     (*end case*))
70 : jhr 2356
71 : cchiw 2845 (*doNormalize:EIN->EIN
72 : cchiw 2844 * Orders EIN, normalizes it, then cleans the summation
73 :     *)
74 :     fun doNormalize e=let
75 :     val _ = testp ["\n in do Normalize"]
76 :     val ordered=Order.orderfn e
77 :     val (n,change)=NE.normalize ordered
78 :     val e'=S.cleanSummation n
79 :     val _ =testp ["original \n =>\n",P.printerE(e),"\n Ordered:\n",P.printerE(ordered),"\n normalized\n=>",P.printerE(n),"\n Move Sums \n =>", P.printerE(e')]
80 : cchiw 2845 in
81 :     e'
82 :     end
83 : cchiw 2507
84 : cchiw 2845 (*foundEIN:EIN.Params*int*int*EIN*HighIL Vars* rhs list* EIN *HighIL Var
85 :     ->int*EIN*int*rhs list
86 :     * Orders EIN, normalizes it, then cleans the summation
87 :     *orig-original EIN
88 :     *)
89 :     fun foundEin(paramCount,place,change,newE,newArgs,done,newEinApp,orig,lhs)= (case (List.nth(paramCount, place))
90 :     of Ein.TEN(0,_)=> (change,orig, place+1, done@[newEinApp])
91 :     | _ => let
92 :     val (c,subst)=App.app(orig,place,newE)
93 :     val _ = testp["\n Apply at ", Int.toString(place),":--",P.printerE(newE),
94 :     "\n","Subst Result:\t", P.printerE(subst), "\n"]
95 :     in (case c
96 :     of 0 => (1,subst, place+length(newArgs), done@newArgs)
97 :     |_ => (use lhs;(*decUse e;*)(1,subst, place+length(newArgs), done@newArgs))
98 :     (*end case*))
99 :     end
100 :     (*end case*))
101 : cchiw 2844
102 : cchiw 2845 (*
103 :     *doRHS: HighIL.var * rhs -> option rhs
104 :     *Looks at each argument to the original EINAPP.
105 :     *If it is another EIN APP calls foundEIN to do application
106 :     * "place"-The Param-id for the EIN operator.
107 :     * Keeps track of the place of the argument in substitution.
108 :     *)
109 :     fun doRHS (lhs, IL.EINAPP (ein, args))=let
110 :     val _ = testp ["\n\n**************************\n\n\n",V.toString lhs,"=="," ---Current: ", P.printerE(ein), (String.concatWith "," (List.map V.toString args))]
111 :     fun rewrite(0,_,_, [], _)= NONE
112 :     | rewrite(_,orig,_, [], args')= SOME[(lhs,IL.EINAPP((doNormalize orig), args'))]
113 :     | rewrite(change,orig, place, e::es,args')=(case (isEin e)
114 :     of NONE => rewrite(change,orig, place+1, es, args'@[e])
115 :     | SOME(IL.EINAPP (newE,newA))=> let
116 :     val Ein.EIN{params, index, body}=orig
117 :     val (change',e',place',done')=foundEin(params,place,change,newE,newA,args',e,orig,lhs)
118 :     in rewrite(change',e',place',es,done')
119 :     end
120 :     | _ => raise Fail"isEin did not work"
121 :     (* end case *))
122 :     in rewrite(0,ein, 0,args,[])
123 : cchiw 2605 end
124 : cchiw 2845 | doRHS _ = NONE
125 : cchiw 2507
126 : jhr 2356 structure Rewrite = RewriteFn (
127 :     struct
128 :     structure IL = IL
129 :     val doAssign = doRHS
130 :     fun doMAssign _ = NONE
131 :     val elimUnusedVars = UnusedElim.reduce
132 :     end)
133 : jhr 1232
134 : jhr 2356 val transform = Rewrite.transform
135 : jhr 1232
136 :     end

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