Debian Codegraph @

lib/mlrisc-lib/MLRISC/ra/risc-ra.sml

raw
1 (*
2 * This functor factors out the machine independent part of the register
3 * allocator. It performs integer and floating register allocation.
4 * This works well for RISC machines; but not applicable to x86.
5 *)
6 functor RISC_RA
7 (structure I : INSTRUCTIONS
8 structure Asm : INSTRUCTION_EMITTER (* where I = I *)
9 where type I.addressing_mode = I.addressing_mode
10 and type I.ea = I.ea
11 and type I.instr = I.instr
12 and type I.instruction = I.instruction
13 and type I.operand = I.operand
14 structure CFG : CONTROL_FLOW_GRAPH (* where I = I and P = Asm.S.P *)
15 where type I.addressing_mode = I.addressing_mode
16 and type I.ea = I.ea
17 and type I.instr = I.instr
18 and type I.instruction = I.instruction
19 and type I.operand = I.operand
20 where type P.Client.pseudo_op = Asm.S.P.Client.pseudo_op
21 and type P.T.Basis.cond = Asm.S.P.T.Basis.cond
22 and type P.T.Basis.div_rounding_mode = Asm.S.P.T.Basis.div_rounding_mode
23 and type P.T.Basis.ext = Asm.S.P.T.Basis.ext
24 and type P.T.Basis.fcond = Asm.S.P.T.Basis.fcond
25 and type P.T.Basis.rounding_mode = Asm.S.P.T.Basis.rounding_mode
26 and type P.T.Constant.const = Asm.S.P.T.Constant.const
27 and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Asm.S.P.T.Extension.ccx
28 and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Asm.S.P.T.Extension.fx
29 and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Asm.S.P.T.Extension.rx
30 and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Asm.S.P.T.Extension.sx
31 and type P.T.I.div_rounding_mode = Asm.S.P.T.I.div_rounding_mode
32 and type P.T.Region.region = Asm.S.P.T.Region.region
33 and type P.T.ccexp = Asm.S.P.T.ccexp
34 and type P.T.fexp = Asm.S.P.T.fexp
35 (* and type P.T.labexp = Asm.S.P.T.labexp *)
36 and type P.T.mlrisc = Asm.S.P.T.mlrisc
37 and type P.T.oper = Asm.S.P.T.oper
38 and type P.T.rep = Asm.S.P.T.rep
39 and type P.T.rexp = Asm.S.P.T.rexp
40 and type P.T.stm = Asm.S.P.T.stm
41 structure InsnProps : INSN_PROPERTIES (* where I = I *)
42 where type I.addressing_mode = I.addressing_mode
43 and type I.ea = I.ea
44 and type I.instr = I.instr
45 and type I.instruction = I.instruction
46 and type I.operand = I.operand
47 structure Rewrite : REWRITE_INSTRUCTIONS (* where I = I *)
48 where type I.addressing_mode = I.addressing_mode
49 and type I.ea = I.ea
50 and type I.instr = I.instr
51 and type I.instruction = I.instruction
52 and type I.operand = I.operand
53 structure SpillInstr : ARCH_SPILL_INSTR (* where I = I *)
54 where type I.addressing_mode = I.addressing_mode
55 and type I.ea = I.ea
56 and type I.instr = I.instr
57 and type I.instruction = I.instruction
58 and type I.operand = I.operand
59
60 (* Spilling heuristics determines which node should be spilled.
61 * You can use Chaitin, ChowHenessey, or one of your own.
62 *)
63 structure SpillHeur : RA_SPILL_HEURISTICS
64
65 (* The Spill module figures out the strategies for inserting
66 * spill code. You can use RASpill, or RASpillWithRenaming,
67 * or write your own if you are feeling adventurous.
68 *)
69 structure Spill : RA_SPILL (* where I = I *)
70 where type I.addressing_mode = I.addressing_mode
71 and type I.ea = I.ea
72 and type I.instr = I.instr
73 and type I.instruction = I.instruction
74 and type I.operand = I.operand
75
76 val architecture : string
77
78 (* Is this a pure instruction *)
79 val pure : I.instruction -> bool
80
81
82 datatype spillOperandKind = SPILL_LOC | CONST_VAL
83 type spill_info (* user-defined abstract type *)
84
85 (* Called before RA begins *)
86 val beforeRA : CFG.cfg -> spill_info
87
88 structure Int :
89 sig
90
91 val avail : CellsBasis.cell list (* list of available registers *)
92 val dedicated : CellsBasis.cell list (* list of registers that are dedicated *)
93
94 val spillLoc :
95 { info:spill_info,
96 an :Annotations.annotations ref,
97 cell:CellsBasis.cell, (* spilled cell *)
98 id :RAGraph.logical_spill_id
99 }
100 -> { opnd: I.ea,
101 kind: spillOperandKind
102 }
103
104 (* Mode for RA optimizations *)
105 val mode : RAGraph.mode
106 end
107
108 structure Float :
109 sig
110
111 val avail : CellsBasis.cell list (* list of available registers *)
112 val dedicated : CellsBasis.cell list (* list of registers that are dedicated *)
113
114 val spillLoc :
115 spill_info * Annotations.annotations ref * RAGraph.logical_spill_id
116 -> I.ea
117
118 (* Mode for RA optimizations *)
119 val mode : RAGraph.mode
120 end
121 ) : CFG_OPTIMIZATION =
122 struct
123 structure CFG = CFG
124 structure I = CFG.I
125 structure P = InsnProps
126 structure C = I.C
127 structure G = RAGraph
128 structure CB = CellsBasis
129
130 (* The generic register allocator *)
131 structure Ra =
132 RegisterAllocator
133 (structure SpillHeuristics = SpillHeur
134 (* (ChowHennessySpillHeur) *)
135 structure Flowgraph = ClusterRA
136 (structure Flowgraph = CFG
137 structure Asm = Asm
138 structure InsnProps = InsnProps
139 structure Spill = Spill
140 )
141 )
142
143 val name = "RISC_RA"
144
145 (* Counters for register allocation *)
146 val intSpillsCnt = MLRiscControl.mkCounter ("ra-int-spills", "RA int spill count")
147 val intReloadsCnt = MLRiscControl.mkCounter ("ra-int-reloads", "RA int reload count")
148 val intRenamesCnt = MLRiscControl.mkCounter ("ra-int-renames", "RA int rename count")
149 val floatSpillsCnt = MLRiscControl.mkCounter ("ra-float-spills", "RA float spill count")
150 val floatReloadsCnt = MLRiscControl.mkCounter ("ra-float-reloads", "RA float reload count")
151 val floatRenamesCnt = MLRiscControl.mkCounter ("ra-float-renames", "RA float rename count")
152
153 fun inc c = c := !c + 1
154
155 fun error msg = MLRiscErrorMsg.error("RISC RA "^architecture,msg)
156
157 (*
158 * Make arithmetic non-overflow trapping.
159 * This makes sure that if we happen to run the compiler for a long
160 * period of time overflowing counters will not crash the compiler.
161 *)
162 fun x + y = Word.toIntX(Word.+(Word.fromInt x, Word.fromInt y))
163 fun x - y = Word.toIntX(Word.-(Word.fromInt x, Word.fromInt y))
164
165 (* GetReg specialized to integer and floating point registers *)
166 fun isDedicated (len, arr, others) r =
167 (r < len andalso Array.sub(arr, r)) orelse List.exists (fn d => r = d) others
168
169 fun mark(arr, _, [], others) = others
170 | mark(arr, len, r::rs, others) = let
171 val r = CellsBasis.registerId r
172 in
173 if r >= len then mark(arr, len, rs, r::others)
174 else (Array.update(arr, r, true); mark(arr, len, rs, others))
175 end
176
177 fun annotate([], i) = i
178 | annotate(a::an, i) = annotate(an, I.ANNOTATION{a=a, i=i})
179
180 local
181 val {low,high} = C.cellRange CellsBasis.GP
182 val arr = Array.array(high+1,false)
183 val others = mark(arr, high+1, Int.dedicated, [])
184 in
185 structure GR = GetReg(val first=low val nRegs=high-low+1
186 val available=map CellsBasis.registerId Int.avail)
187 val dedicatedR : int -> bool = isDedicated (high+1, arr, others)
188 end
189
190 fun getRegLoc(S, an, cell, Ra.FRAME loc) =
191 Int.spillLoc{info=S, an=an, cell=cell, id=loc}
192 | getRegLoc _ = error "getRegLoc"
193
194 fun copy((rds as [d], rss as [s]), I.COPY{sz, ...}) =
195 if CB.sameColor(d,s) then []
196 else [I.COPY{k=CB.GP, sz=sz, dst=rds, src=rss, tmp=NONE}]
197 | copy((rds, rss), I.COPY{tmp, sz, ...}) =
198 [I.COPY{k=CB.GP, sz=sz, dst=rds, src=rss, tmp=tmp}]
199 | copy _ = error "copy: COPY?"
200
201 fun spillR S {annotations, kill=true, reg, spillLoc, instr} =
202 if pure instr then {code=[], proh=[], newReg=NONE}
203 else spillR S {annotations=annotations,kill=false,
204 spillLoc=spillLoc,
205 reg=reg,instr=instr}
206 | spillR S {annotations=an, kill, reg, spillLoc, instr} = let
207 fun annotate([], i) = i
208 | annotate(a::an, i) = annotate(an, I.ANNOTATION{a=a, i=i})
209
210 (* preserve annotation on instruction *)
211 fun spill(instrAn, I.ANNOTATION{a, i}) = spill(a::instrAn, i)
212 | spill(instrAn, I.KILL{regs, spilled}) =
213 {code=
214 [annotate
215 (instrAn,
216 I.KILL {regs=C.rmvReg(reg, regs),
217 spilled=C.addReg(reg, spilled)})],
218 proh = [],
219 newReg=NONE}
220 | spill(instrAn, I.LIVE _) = error "spillR: LIVE"
221 | spill(_, I.COPY _) = error "spillR: COPY"
222 | spill(instrAn, I.INSTR _) = let
223 val {opnd=spillLoc:I.ea, kind} = getRegLoc (S, an, reg, spillLoc)
224 in
225 inc intSpillsCnt;
226 SpillInstr.spill CB.GP (instr, reg, spillLoc)
227 end
228 in spill([], instr)
229 end
230
231 (* spill src at the spill location for reg i.e. spillLoc *)
232 fun spillReg S {annotations=an,src,reg,spillLoc} =
233 (inc intSpillsCnt;
234 #code(SpillInstr.spillToEA CB.GP (src, #opnd(getRegLoc(S, an, reg, spillLoc)))))
235
236
237 (* Spill the temporary associated with a copy *)
238 fun spillTmp S {annotations=an, reg, copy=I.COPY{k=CB.GP, sz, tmp, dst, src}, spillLoc} = let
239 val loc = #opnd(getRegLoc(S, an, reg, spillLoc))
240 in
241 inc intSpillsCnt;
242 I.COPY{k=CB.GP, sz=sz, tmp=SOME loc, dst=dst, src=src}
243 end
244 | spillTmp _ _ = error "spillTmp"
245
246 (* Rename integer register *)
247 fun renameR{fromSrc,toSrc,instr} =
248 let val _ = inc intRenamesCnt
249 val instr' = Rewrite.rewriteUse(instr, fromSrc, toSrc)
250 in {code=[instr'], proh=[], newReg=SOME toSrc}
251 end
252
253 (* Reload integer register *)
254 fun reloadR S {annotations=an, reg, spillLoc, instr} = let
255 fun reload(instrAn, I.ANNOTATION{a, i}) = reload(a::instrAn, i)
256 | reload(instrAn, I.LIVE{regs, spilled}) =
257 {code=[I.LIVE{regs=C.rmvReg(reg, regs), spilled=C.addReg(reg, spilled)}],
258 proh=[],
259 newReg=NONE}
260 | reload(_, I.KILL _) = error "reloadR: KILL"
261 | reload (_, I.COPY _) = error "reloadR: COPY"
262 | reload(instrAn, instr as I.INSTR _) = let
263 val spillLoc = #opnd (getRegLoc(S, an, reg, spillLoc))
264 in
265 inc intReloadsCnt;
266 SpillInstr.reload CB.GP (instr, reg, spillLoc)
267 end
268 in reload([], instr)
269 end
270
271 (* reload the register dst from the spill location for reg, i.e. spillLoc *)
272 fun reloadReg S {annotations=an,reg,dst,spillLoc} =
273 (inc intReloadsCnt;
274 #code(SpillInstr.reloadFromEA CB.GP (dst, #opnd(getRegLoc(S, an, reg, spillLoc)))))
275
276
277 (*-------------------------------------------------------------*)
278 local
279 val {low,high} = C.cellRange CellsBasis.FP
280 val arr = Array.array(high+1,false)
281 val others = mark(arr, high+1, Float.dedicated, [])
282 in
283 structure FR = GetReg(val first=low val nRegs=high-low+1
284 val available=map CellsBasis.registerId Float.avail)
285 val dedicatedF : int -> bool = isDedicated(high+1, arr, others)
286 end
287
288 fun getFregLoc(S, an, Ra.FRAME loc) = Float.spillLoc(S, an, loc)
289 | getFregLoc _ = error "getFregLoc"
290
291 fun fcopy((rds as [d], rss as [s]), I.COPY{sz, ...}) =
292 if CB.sameColor(d,s) then []
293 else [I.COPY{k=CB.FP, sz=sz, dst=rds, src=rss, tmp=NONE}]
294 | fcopy((rds, rss), I.COPY{tmp, sz, ...}) =
295 [I.COPY{k=CB.FP, sz=sz, dst=rds, src=rss, tmp=tmp}]
296 | fcopy _ = error "fcopy: COPY?"
297
298 (* Spill floating point register *)
299 fun spillF S {annotations, kill=true, reg, spillLoc, instr} =
300 if pure instr then {code=[], proh=[], newReg=NONE}
301 else spillF S {annotations=annotations,kill=false,
302 spillLoc=spillLoc, reg=reg,instr=instr}
303 | spillF S {annotations=an, kill, reg, spillLoc, instr} = let
304 (* preserve annotation on instruction *)
305 fun spill(instrAn, I.ANNOTATION{a, i}) = spill(a::instrAn, i)
306 | spill(instrAn, I.KILL{regs, spilled}) =
307 {code=
308 [annotate
309 (instrAn,
310 I.KILL {regs=C.rmvFreg(reg, regs),
311 spilled=C.addFreg(reg, spilled)})],
312 proh = [],
313 newReg=NONE}
314 | spill(instrAn, I.LIVE _) = error "spillF: LIVE"
315 | spill(_, I.COPY _) = error "spillF: COPY"
316 | spill(instrAn, I.INSTR _) =
317 (inc floatSpillsCnt;
318 SpillInstr.spill CB.FP (instr, reg, getFregLoc(S, an, spillLoc)))
319 in spill([], instr)
320 end
321
322 (* spill src at the spill location for reg, i.e. spillLoc *)
323 fun spillFreg S {annotations=an,reg,src,spillLoc} =
324 (inc floatSpillsCnt;
325 #code(SpillInstr.spillToEA CB.FP (src, getFregLoc(S, an, spillLoc))))
326
327 (* Spill the temporary associated with a copy *)
328 fun spillFtmp S {annotations=an, reg, copy=I.COPY{k=CB.FP, sz, tmp, dst, src}, spillLoc} = let
329 val loc = getFregLoc(S, an, spillLoc)
330 in
331 inc floatSpillsCnt;
332 I.COPY{k=CB.FP, sz=sz, tmp=SOME loc, dst=dst, src=src}
333 end
334 | spillFtmp _ _ = error "spillFtmp"
335
336
337 (* Rename floating point register *)
338 fun renameF{fromSrc,toSrc,instr} =
339 let val _ = inc floatRenamesCnt
340 val instr' = Rewrite.frewriteUse(instr, fromSrc, toSrc)
341 in {code=[instr'], proh=[], newReg=SOME toSrc}
342 end
343
344 (* Reload floating point register *)
345 fun reloadF S {annotations=an, reg, spillLoc, instr} = let
346 fun reload(instrAn, I.ANNOTATION{a,i}) = reload(a::instrAn, i)
347 | reload(instrAn, I.LIVE{regs, spilled}) =
348 {code=[I.LIVE{regs=C.rmvFreg(reg, regs), spilled=C.addFreg(reg, spilled)}],
349 proh=[],
350 newReg=NONE}
351 | reload(_, I.KILL _) = error "reloadF: KILL"
352 | reload (_, I.COPY _) = error "reloadF: COPY"
353 | reload(instrAn, instr as I.INSTR _) =
354 (inc floatReloadsCnt;
355 SpillInstr.reload CB.FP (instr, reg, getFregLoc(S, an, spillLoc)))
356 in reload([], instr)
357 end
358
359 (* reload register dst from the spill location for reg, i.e. spillLoc *)
360 fun reloadFreg S {annotations=an,reg,dst,spillLoc} =
361 (inc floatReloadsCnt;
362 #code (SpillInstr.reloadFromEA CB.FP (dst, getFregLoc(S, an, spillLoc))))
363
364 val KR = length Int.avail
365 val KF = length Float.avail
366
367 fun params S =
368 [ { cellkind = CellsBasis.GP,
369 getreg = GR.getreg,
370 spill = spillR S,
371 spillSrc = spillReg S,
372 spillCopyTmp = spillTmp S,
373 reload = reloadR S,
374 reloadDst = reloadReg S,
375 renameSrc = renameR,
376 K = KR,
377 dedicated = dedicatedR,
378 copyInstr = copy,
379 spillProh = [],
380 memRegs = [],
381 mode = Int.mode
382 } : Ra.raClient,
383 { cellkind = CellsBasis.FP,
384 getreg = FR.getreg,
385 spill = spillF S,
386 spillSrc = spillFreg S,
387 spillCopyTmp = spillFtmp S,
388 reload = reloadF S,
389 reloadDst = reloadFreg S,
390 renameSrc = renameF,
391 K = KF,
392 dedicated = dedicatedF,
393 copyInstr = fcopy,
394 spillProh = [],
395 memRegs = [],
396 mode = Float.mode
397 } : Ra.raClient
398 ] : Ra.raClient list
399
400 fun run cluster = let
401 val S = beforeRA cluster
402 in
403 GR.reset();
404 FR.reset();
405 Ra.ra (params S) cluster
406 end
407
408 end
409