Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/MLRISC/Glue/mlrisc-ssa-optimizer.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/Glue/mlrisc-ssa-optimizer.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 409 - (view) (download)

1 : monnier 409 (*
2 :     * SSA optimizer for doing experiments
3 :     *)
4 :    
5 :     functor SSAOptimizerFn
6 :     (structure Asm : INSTRUCTION_EMITTER
7 :     structure F : FLOWGRAPH
8 :     structure P : INSN_PROPERTIES
9 :     structure SP : SSA_PROPERTIES
10 :     structure FreqProps : FREQUENCY_PROPERTIES
11 :     sharing P.I = SP.I = Asm.I = F.I = FreqProps.I
12 :     sharing F.P = Asm.P
13 :     val copyProp : F.cluster -> F.cluster
14 :     ) : SSA_OPTIMIZER =
15 :     struct
16 :    
17 :     structure F = F
18 :     structure I = F.I
19 :     structure B = F.B
20 :     structure Ctrl = MLRISC_Control
21 :    
22 :     val view_IR = Ctrl.getFlag "view-IR"
23 :     val verbose = Ctrl.getFlag "verbose"
24 :     val viewer = Ctrl.getString "viewer"
25 :     val min_blocks = Ctrl.getInt "min-blocks"
26 :    
27 :     fun error msg = MLRiscErrorMsg.error("SSAOptimizer",msg)
28 :    
29 :     structure GraphViewer = GraphViewerFn(AllDisplaysFn(val viewer = viewer))
30 :    
31 :     structure FormatInsn = FormatInstructionFn(Asm)
32 :    
33 :     structure CFG = ControlFlowGraphFn
34 :     (structure I = I
35 :     structure B = B
36 :     structure P = F.P
37 :     structure GraphImpl = DirectedGraph
38 :     structure Asm = Asm
39 :     structure Ctrl = Ctrl
40 :     )
41 :    
42 :     structure CFG2Cluster = CFG2ClusterFn
43 :     (structure CFG = CFG
44 :     structure F = F
45 :     )
46 :    
47 :     structure Cluster2CFG = Cluster2CFGFn
48 :     (structure CFG = CFG
49 :     structure F = F
50 :     structure P = P
51 :     )
52 :    
53 :     structure Dom = DominatorTreeFn(DirectedGraph)
54 :    
55 :     structure CDG = ControlDependenceGraphFn
56 :     (structure Dom = Dom
57 :     structure GraphImpl = DirectedGraph
58 :     )
59 :    
60 :     structure Loop = LoopStructureFn
61 :     (structure Dom = Dom
62 :     structure GraphImpl = DirectedGraph
63 :     )
64 :    
65 :     structure Util = CFGUtilFn
66 :     (structure CFG = CFG
67 :     structure P = P
68 :     )
69 :    
70 :     structure IR = MLRISC_IRFn
71 :     (structure CFG = CFG
72 :     structure CDG = CDG
73 :     structure Loop = Loop
74 :     structure GraphViewer = GraphViewer
75 :     structure Util = Util
76 :     structure Ctrl = Ctrl
77 :     )
78 :    
79 :     structure Guess = StaticBranchPredictionFn(structure IR = IR
80 :     structure Props = P
81 :     structure FreqProps = FreqProps
82 :     )
83 :    
84 :     structure Liveness = LivenessAnalysisFn(CFG)
85 :    
86 :     structure SSA = SSAFn
87 :     (structure CFG = CFG
88 :     structure Dom = Dom
89 :     structure SP = SP
90 :     structure P = P
91 :     structure RTL = SP.RTL
92 :     structure FormatInsn = FormatInsn
93 :     structure GraphImpl = DirectedGraph
94 :     structure Ctrl = Ctrl
95 :     )
96 :    
97 :     structure CFG2SSA = CFG2SSAFn
98 :     (structure SSA = SSA
99 :     structure Liveness = Liveness
100 :     )
101 :    
102 :     structure Reshape = ReshapeBranchesFn(structure IR = IR
103 :     structure P = P)
104 :    
105 :     structure InsertPreheaders = InsertPreheadersFn(structure IR = IR
106 :     structure P = P
107 :     structure Ctrl = Ctrl)
108 :    
109 :     structure SSADCE = SSADeadCodeElimFn(SSA)
110 :    
111 :     structure CF = SSAConstantFoldingFn(SSA)
112 :    
113 :     structure GVN = SSAGlobalValueNumberingFn(CF)
114 :    
115 :     structure SSAGVN = SSAGVNFn(structure GVN = GVN
116 :     val leaveBehindCopy = false
117 :     val foldGlobalConstants = true)
118 :    
119 :     structure SSAGVNL = SSAGVNFn(structure GVN = GVN
120 :     val leaveBehindCopy = false
121 :     val foldGlobalConstants = false)
122 :    
123 :     structure SSAGVN' = SSAGVNFn(structure GVN = GVN
124 :     val leaveBehindCopy = false
125 :     val foldGlobalConstants = true)
126 :    
127 :     structure SSAGCM = SSAGlobalCodeMotionFn(SSA)
128 :     (* structure SSAGCM2 = SSAGlobalCodeMotion2Fn(SSA) *)
129 :     structure Depressurize = SSADepressurizeFn(SSA)
130 :    
131 :     structure SSALiveness = SSALivenessFn(SSA)
132 :    
133 :     structure SSA2CFG = SSA2CFGFn
134 :     (structure SSA = SSA
135 :     structure Liveness = SSALiveness
136 :     structure P = P
137 :     structure Util = Util
138 :     structure Ctrl = Ctrl
139 :     )
140 :    
141 :     fun view phase ir = if !view_IR then IR.view phase ir else ()
142 :    
143 :     fun optimize cluster =
144 :     let datatype rep = IR of IR.IR
145 :     | CLUSTER of F.cluster
146 :     | SSA of SSA.ssa
147 :     fun doPhase "copy-prop" (CLUSTER c) = CLUSTER(copyProp c)
148 :     | doPhase "cluster->cfg" (CLUSTER c) = IR(Cluster2CFG.cluster2cfg c)
149 :     | doPhase "cfg->cluster" (IR cfg) =
150 :     CLUSTER(CFG2Cluster.cfg2cluster{cfg=cfg,relayout=false})
151 :     | doPhase "guess" (r as IR ir) =
152 :     (Guess.profile {loopMultiplier=10} ir; r)
153 :     | doPhase "reshape" (r as IR ir) = (Reshape.reshapeBranches ir; r)
154 :     | doPhase "insert-preheaders" (r as IR ir) =
155 :     (InsertPreheaders.insert_preheaders ir; r)
156 :     | doPhase "split-critical-edges" (r as IR ir) =
157 :     (Util.splitAllCriticalEdges ir; r)
158 :     | doPhase "view-cfg" (r as IR ir) = (view "cfg" ir; r)
159 :     | doPhase "view-dom" (r as IR ir) = (view "dom" ir; r)
160 :     | doPhase "view-doms" (r as IR ir) = (view "doms" ir; r)
161 :     | doPhase "view-cdg" (r as IR ir) = (view "cdg" ir; r)
162 :     | doPhase "view-loop" (r as IR ir) = (view "loop" ir; r)
163 :     | doPhase "view-ssacfg" (r as SSA ssa) =
164 :     (if !view_IR then GraphViewer.view (SSA.viewAsCFG ssa) else (); r)
165 :     | doPhase "view-ssa" (r as SSA ssa) =
166 :     (if !view_IR then GraphViewer.view (SSA.viewAsSSA ssa) else (); r)
167 :     | doPhase "cfg->ssa" (IR ir) = SSA(CFG2SSA.buildSSA(ir,IR.dom ir))
168 :     | doPhase "ssa-dce" (SSA ssa) = SSA(SSADCE.optimize ssa)
169 :     | doPhase "ssa-gvn" (SSA ssa) = SSA(SSAGVN.optimize ssa)
170 :     | doPhase "ssa-gvnl" (SSA ssa) = SSA(SSAGVNL.optimize ssa)
171 :     | doPhase "ssa-gvn'" (SSA ssa) = SSA(SSAGVN'.optimize ssa)
172 :     | doPhase "ssa-gcm" (SSA ssa) = SSA(SSAGCM.optimize ssa)
173 :     (* | doPhase "ssa-gcm2" (SSA ssa) = SSA(SSAGCM2.optimize ssa) *)
174 :     | doPhase "ssa-dep" (SSA ssa) = SSA(Depressurize.optimize ssa)
175 :     | doPhase "gvn" (r as SSA ssa) =
176 :     (GVN.computeValueNumbers ssa; r)
177 :     | doPhase "ssa->cfg" (SSA ssa) = IR(SSA2CFG.buildCFG ssa)
178 :     | doPhase phase _ = error(phase)
179 :     fun doPhases [] (CLUSTER c) = c
180 :     | doPhases [] _ = error "cluster needed"
181 :     | doPhases (phase::phases) ir =
182 :     let fun pr msg = TextIO.output(TextIO.stdErr,msg)
183 :     val _ = if !verbose then pr("[ start "^phase^"]") else ();
184 :     val timer = Timer.startCPUTimer()
185 :     val ir = doPhase phase ir handle e =>
186 :     (print("[ "^phase^": uncaught exception: "
187 :     ^exnName e^" ]\n"); raise e)
188 :     val {gc,sys,usr} = Timer.checkCPUTimer timer
189 :     val _ = if !verbose then
190 :     pr("[ end "^phase^" usr="^Time.toString usr^
191 :     " sys="^Time.toString sys^
192 :     " gc="^Time.toString gc^"]\n") else ();
193 :     in doPhases phases ir end
194 :    
195 :     val F.CLUSTER{blocks,...} = cluster
196 :     fun isAllGC([],gc,n) = (gc,n)
197 :     | isAllGC(F.BBLOCK{succ,pred,...}::bs,gc,n) =
198 :     isAllGC(bs,gc andalso (case (!succ,!pred) of
199 :     ([_],[_]) => true | _ => false),n+1)
200 :     | isAllGC(_::bs,gc,n) = isAllGC(bs,gc,n)
201 :     in case isAllGC(blocks,true,0) of
202 :     (true,_) => cluster
203 :     | (false,n) =>
204 :     if n >= !min_blocks then
205 :     doPhases (!Ctrl.mlrisc_phases) (CLUSTER cluster)
206 :     else
207 :     cluster
208 :     end
209 :    
210 :     fun codegen cluster = if !Ctrl.mlrisc then optimize cluster else cluster
211 :    
212 :     end

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