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/branches/SMLNJ/src/MLRISC/Glue/mlrisc-ssa-optimizer.sml
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 498 - (view) (download)

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

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