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 469 - (view) (download)
Original Path: sml/branches/SMLNJ/src/MLRISC/Glue/mlrisc-ssa-optimizer.sml

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

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