Debian Codegraph @

lib/mlrisc-lib/MLRISC/backpatch/spanDep.sml

raw
1 (*
2 * This version of the span dependency resolution also fill delay slots
3 * using a few simple strategies.
4 *
5 * Assumption: Instructions are 32bits.
6 *
7 * Allen
8 *)
9
10 functor SpanDependencyResolution
11 (structure Emitter : INSTRUCTION_EMITTER
12 structure CFG : CONTROL_FLOW_GRAPH (* where I = Emitter.I and P = Emitter.S.P *)
13 where type I.addressing_mode = Emitter.I.addressing_mode
14 and type I.ea = Emitter.I.ea
15 and type I.instr = Emitter.I.instr
16 and type I.instruction = Emitter.I.instruction
17 and type I.operand = Emitter.I.operand
18 where type P.Client.pseudo_op = Emitter.S.P.Client.pseudo_op
19 and type P.T.Basis.cond = Emitter.S.P.T.Basis.cond
20 and type P.T.Basis.div_rounding_mode = Emitter.S.P.T.Basis.div_rounding_mode
21 and type P.T.Basis.ext = Emitter.S.P.T.Basis.ext
22 and type P.T.Basis.fcond = Emitter.S.P.T.Basis.fcond
23 and type P.T.Basis.rounding_mode = Emitter.S.P.T.Basis.rounding_mode
24 and type P.T.Constant.const = Emitter.S.P.T.Constant.const
25 and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.ccx
26 and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.fx
27 and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.rx
28 and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.sx
29 and type P.T.I.div_rounding_mode = Emitter.S.P.T.I.div_rounding_mode
30 and type P.T.Region.region = Emitter.S.P.T.Region.region
31 and type P.T.ccexp = Emitter.S.P.T.ccexp
32 and type P.T.fexp = Emitter.S.P.T.fexp
33 (* and type P.T.labexp = Emitter.S.P.T.labexp *)
34 and type P.T.mlrisc = Emitter.S.P.T.mlrisc
35 and type P.T.oper = Emitter.S.P.T.oper
36 and type P.T.rep = Emitter.S.P.T.rep
37 and type P.T.rexp = Emitter.S.P.T.rexp
38 and type P.T.stm = Emitter.S.P.T.stm
39 structure Jumps : SDI_JUMPS (* where I = CFG.I *)
40 where type I.addressing_mode = CFG.I.addressing_mode
41 and type I.ea = CFG.I.ea
42 and type I.instr = CFG.I.instr
43 and type I.instruction = CFG.I.instruction
44 and type I.operand = CFG.I.operand
45 structure DelaySlot : DELAY_SLOT_PROPERTIES (* where I = CFG.I *)
46 where type I.addressing_mode = CFG.I.addressing_mode
47 and type I.ea = CFG.I.ea
48 and type I.instr = CFG.I.instr
49 and type I.instruction = CFG.I.instruction
50 and type I.operand = CFG.I.operand
51 structure Props : INSN_PROPERTIES (* where I = CFG.I *)
52 where type I.addressing_mode = CFG.I.addressing_mode
53 and type I.ea = CFG.I.ea
54 and type I.instr = CFG.I.instr
55 and type I.instruction = CFG.I.instruction
56 and type I.operand = CFG.I.operand
57 structure Asm : INSTRUCTION_EMITTER (* where I = CFG.I and S = Emitter.S *)
58 where type I.addressing_mode = CFG.I.addressing_mode
59 and type I.ea = CFG.I.ea
60 and type I.instr = CFG.I.instr
61 and type I.instruction = CFG.I.instruction
62 and type I.operand = CFG.I.operand
63 where type S.P.Client.pseudo_op = Emitter.S.P.Client.pseudo_op
64 and type S.P.T.Basis.cond = Emitter.S.P.T.Basis.cond
65 and type S.P.T.Basis.div_rounding_mode = Emitter.S.P.T.Basis.div_rounding_mode
66 and type S.P.T.Basis.ext = Emitter.S.P.T.Basis.ext
67 and type S.P.T.Basis.fcond = Emitter.S.P.T.Basis.fcond
68 and type S.P.T.Basis.rounding_mode = Emitter.S.P.T.Basis.rounding_mode
69 and type S.P.T.Constant.const = Emitter.S.P.T.Constant.const
70 and type ('s,'r,'f,'c) S.P.T.Extension.ccx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.ccx
71 and type ('s,'r,'f,'c) S.P.T.Extension.fx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.fx
72 and type ('s,'r,'f,'c) S.P.T.Extension.rx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.rx
73 and type ('s,'r,'f,'c) S.P.T.Extension.sx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.sx
74 and type S.P.T.I.div_rounding_mode = Emitter.S.P.T.I.div_rounding_mode
75 and type S.P.T.Region.region = Emitter.S.P.T.Region.region
76 and type S.P.T.ccexp = Emitter.S.P.T.ccexp
77 and type S.P.T.fexp = Emitter.S.P.T.fexp
78 (* and type S.P.T.labexp = Emitter.S.P.T.labexp *)
79 and type S.P.T.mlrisc = Emitter.S.P.T.mlrisc
80 and type S.P.T.oper = Emitter.S.P.T.oper
81 and type S.P.T.rep = Emitter.S.P.T.rep
82 and type S.P.T.rexp = Emitter.S.P.T.rexp
83 and type S.P.T.stm = Emitter.S.P.T.stm
84 and type ('a,'b,'c,'d) S.stream = ('a,'b,'c,'d) Emitter.S.stream
85 ) : BBSCHED =
86 struct
87
88 structure CFG = CFG
89 structure E = Emitter
90 structure I = CFG.I
91 structure C = I.C
92 structure J = Jumps
93 structure P = CFG.P
94 structure D = DelaySlot
95 structure G = Graph
96 structure A = Array
97
98 fun error msg = MLRiscErrorMsg.error("SpanDependencyResolution",msg)
99
100 datatype code =
101 SDI of {size : int ref, (* variable sized *)
102 insn : I.instruction}
103 | FIXED of {size: int, (* size of fixed instructions *)
104 insns: I.instruction list}
105 | BRANCH of {insn : code list, (* instruction with delay slot*)
106 branchSize : int,
107 fillSlot : bool ref}
108 | DELAYSLOT of {insn : code list, (* instruction in delay slot *)
109 fillSlot : bool ref}
110 | CANDIDATE of (* two alternatives *)
111 { oldInsns : code list, (* without delay slot filling *)
112 newInsns : code list, (* when delay slot is filled *)
113 fillSlot : bool ref (* should we fill the delay slot? *)
114 }
115
116 datatype compressed =
117 PSEUDO of P.pseudo_op
118 | LABEL of Label.label
119 | CODE of Label.label * code list
120
121 datatype cluster = CLUSTER of {comp : compressed list}
122
123 val clusterList : cluster list ref = ref []
124 val dataList : P.pseudo_op list ref = ref []
125 fun cleanUp() = (clusterList := []; dataList := [])
126
127 fun bbsched(G.GRAPH graph, blocks : CFG.node list) = let
128 val blocks = map #2 blocks
129
130 fun maxBlockId (CFG.BLOCK{id, ...}::rest, curr) =
131 if id > curr then maxBlockId(rest, id) else maxBlockId(rest, curr)
132 | maxBlockId([], curr) = curr
133 val N = maxBlockId(blocks, #capacity graph ())
134
135 (* Order of blocks in code layout *)
136 val blkOrder = Array.array(N, 0)
137
138 (* Maps blknum -> label at the position of the second instruction *)
139 (* This is incase the first instruction gets used to fill a delay slot *)
140 val dummy = Label.anon ()
141 val labelMap = A.array(N, dummy)
142
143 (* enter labels into the labelMap *)
144 fun enterLabels(blocks) =
145 List.app
146 (fn CFG.BLOCK{id, ...} => Array.update(labelMap, id, Label.anon ()))
147 blocks
148
149 (* create block order *)
150 fun blockOrder(blocks) = let
151 fun order(CFG.BLOCK{id, ...}, n) = (Array.update(blkOrder, id, n); n+1)
152 in List.foldl order 0 blocks
153 end
154
155 fun isFallthrough(blk1, blk2) =
156 Array.sub(blkOrder, blk1) + 1 = Array.sub(blkOrder, blk2)
157
158 fun isBackwards(blk1, blk2) =
159 Array.sub(blkOrder, blk2) <= Array.sub(blkOrder, blk1)
160
161 (* zero length copy instruction *)
162 fun isEmptyCopy instr =
163 Props.instrKind(instr) = Props.IK_COPY
164 andalso J.sdiSize(instr, Label.addrOf, 0) = 0
165
166 (* Find the target of a block, and return the first instruction and
167 * its associated label.
168 *)
169 fun findTarget(blknum, [CFG.BLOCK{id=id1, insns=insns1, ...},
170 CFG.BLOCK{id=id2, insns=insns2, ...}]) = let
171 fun extract(blknum, insns) = let
172 (* skip over empty copies *)
173 fun find [] = NONE
174 | find(instrs as instr::rest) =
175 if isEmptyCopy instr then find rest else find' rest
176
177 (* Okay, we are now guaranteed that the remaining
178 * instructions will not be used in the delay slot of
179 * the current block. Find the first instruction.
180 *)
181 and find' [first] = SOME(first, A.sub(labelMap,blknum))
182 | find' [] = NONE
183 | find' (_::rest) = find' rest
184 in
185 case insns
186 of jmp::rest =>
187 if Props.instrKind jmp = Props.IK_JUMP then find rest
188 else find insns
189 | [] => NONE (* no first instruction *)
190 end
191 in
192 if isFallthrough(blknum, id1) then extract(id2, !insns2)
193 else if isFallthrough(blknum, id2) then extract(id1, !insns1)
194 else NONE
195 end
196 | findTarget _ = NONE
197
198
199
200 fun compress [] = []
201 | compress (CFG.BLOCK{id, align, labels, insns, ...}::rest) = let
202
203 val succ = map (#node_info graph) (#succ graph id)
204
205 val backward =
206 List.exists
207 (fn CFG.BLOCK{id=id1, ...} => isBackwards(id, id1))
208 succ
209
210 (* build the code list *)
211 fun scan([],nonSdiInstrs,nonSdiSize,code) =
212 group(nonSdiSize,nonSdiInstrs,code)
213 | scan(instr::instrs,nonSdiInstrs,nonSdiSize,code) =
214 let val {n,nOn,nOff,nop} = D.delaySlot{instr=instr,backward=backward}
215 in case (nOff,instrs) of
216 (D.D_ALWAYS,delaySlot::rest) =>
217 if D.delaySlotCandidate{jmp=instr,
218 delaySlot=delaySlot} andalso
219 not(D.conflict{src=delaySlot,dst=instr})
220 then scan(rest,[],0,
221 mkCandidate1(instr,delaySlot)::
222 group(nonSdiSize,nonSdiInstrs,code))
223 else scanSdi(instr,instrs,nonSdiInstrs,nonSdiSize,code)
224 | _ => scanSdi(instr,instrs,nonSdiInstrs,nonSdiSize,code)
225 end
226 and scanSdi(instr,instrs,nonSdiInstrs,nonSdiSize,code) =
227 let val s = J.minSize instr
228 in if J.isSdi instr then
229 scan(instrs,[],0,SDI{size=ref s,insn=instr}::
230 group(nonSdiSize,nonSdiInstrs,code))
231 else scan(instrs,instr::nonSdiInstrs,nonSdiSize+s,code)
232 end
233 and group(0,[],code) = code
234 | group(size,insns,code) = FIXED{size=size,insns=insns}::code
235
236 and buildList instrs = scan'(instrs,[],0,[])
237
238 and scan'([],nonSdiInstrs,nonSdiSize,code) =
239 group(nonSdiSize,nonSdiInstrs,code)
240 | scan'(instr::instrs,nonSdiInstrs,nonSdiSize,code) =
241 let val s = J.minSize instr
242 in if J.isSdi instr then
243 scan'(instrs,[],0,SDI{size=ref s,insn=instr}::
244 group(nonSdiSize,nonSdiInstrs,code))
245 else scan'(instrs,instr::nonSdiInstrs,nonSdiSize+s,code)
246 end
247
248 (*
249 * Create a branch delay slot candidate sequence.
250 * jmp is the normal jump instruction; jmp' is the
251 * jump instruction when the delay slot is active.
252 *)
253 and mkCandidate1(jmp,delaySlot) =
254 let val fillSlot = ref true
255 val jmp' = D.enableDelaySlot{n=false,nop=false,instr=jmp}
256 in CANDIDATE{newInsns=
257 [BRANCH{branchSize=J.minSize jmp',
258 insn=buildList [jmp'],
259 fillSlot=fillSlot},
260 DELAYSLOT{insn=buildList [delaySlot],
261 fillSlot=fillSlot}],
262 oldInsns=buildList [jmp,delaySlot],
263 fillSlot=fillSlot}
264 end
265
266 (*
267 * Create a branch delay slot candidate sequence.
268 * jmp is the normal jump instruction; jmp' is the
269 * jump instruction when the delay slot is active.
270 *)
271 and mkCandidate2(jmp,delaySlot,label) =
272 let val fillSlot = ref true
273 val jmp' = D.setTarget(
274 D.enableDelaySlot{n=true,nop=false,instr=jmp},
275 label)
276 in CANDIDATE{newInsns=
277 [BRANCH{branchSize=J.minSize jmp',
278 insn=buildList [jmp'],
279 fillSlot=fillSlot},
280 DELAYSLOT{insn=buildList [delaySlot],
281 fillSlot=fillSlot}],
282 oldInsns=buildList [jmp],
283 fillSlot=fillSlot}
284 end
285
286 (*
287 * Try different strategies for delay slot filling
288 *)
289 and fitDelaySlot(jmp,body) =
290 (case body of (* remove empty copies *)
291 [] => fitDelaySlot'(jmp,body)
292 | prev::rest =>
293 if isEmptyCopy prev
294 then fitDelaySlot(jmp,rest)
295 else fitDelaySlot'(jmp,body)
296 )
297
298 and fitDelaySlot'(jmp,body) =
299 let val {n,nOn,nOff,nop} = D.delaySlot{instr=jmp,backward=backward}
300 (*
301 * Use the previous instruction to fill the delay slot
302 *)
303 fun strategy1() =
304 case (nOff,body) of
305 (D.D_ALWAYS,delaySlot::body) =>
306 if not(D.delaySlotCandidate{jmp=jmp,
307 delaySlot=delaySlot}) orelse
308 D.conflict{src=delaySlot,dst=jmp}
309 then strategy2()
310 else scan(body,[],0,
311 [mkCandidate1(eliminateNop jmp,delaySlot)])
312 | _ => strategy2()
313 (*
314 * Use the first instruction in the target block to fill
315 * the delay slot.
316 * BUG FIX: note this is unsafe if this first instruction
317 * is also used to fill the delay slot in the target block!
318 *)
319 and strategy2() =
320 case (nOn,findTarget(id,succ)) of
321 (D.D_TAKEN,SOME(delaySlot,label)) =>
322 if not(D.delaySlotCandidate{jmp=jmp,
323 delaySlot=delaySlot}) orelse
324 D.conflict{src=delaySlot,dst=jmp}
325 then strategy3()
326 else scan(body,[],0,
327 [mkCandidate2(eliminateNop jmp,delaySlot,label)])
328 | _ => strategy3()
329
330 (*
331 * If nop is on and if the delay slot is only active on
332 * the fallsthru branch, then turn nullify on and eliminate
333 * the delay slot
334 *)
335 and strategy3() = scan(eliminateNop(jmp)::body,[],0,[])
336
337 and eliminateNop(jmp) =
338 case (nop,nOn) of
339 (true,(D.D_FALLTHRU | D.D_NONE)) =>
340 D.enableDelaySlot{n=true,nop=false,instr=jmp}
341 | _ => jmp
342
343 in strategy1()
344 end
345
346 and process(instrs, others) = let
347 fun alignIt(chunks) =
348 (case !align of NONE => chunks | SOME p => PSEUDO(p)::chunks)
349 val code =
350 (case instrs
351 of [] => []
352 | jmp::body =>
353 (case Props.instrKind jmp
354 of Props.IK_JUMP => fitDelaySlot(jmp, body)
355 | _ => scan(instrs, [], 0, [])
356 (*esac*))
357 (*esac*))
358 in
359 alignIt
360 (map LABEL (!labels) @
361 CODE (A.sub(labelMap, id), code) :: others)
362
363 end
364 in
365 process(!insns,compress rest)
366 end (* compress *)
367
368 val CFG.INFO{data, ...} = #graph_info graph
369 in
370 blockOrder(blocks);
371 enterLabels(blocks);
372 clusterList := CLUSTER{comp=compress blocks} :: !clusterList;
373 dataList := !data @ !dataList
374 end (* bbsched *)
375
376
377
378
379 fun finish () = let
380 fun labels(PSEUDO pOp::rest, loc) =
381 (P.adjustLabels(pOp, loc); labels(rest, loc+P.sizeOf(pOp, loc)))
382 | labels(LABEL lab::rest, loc) =
383 (Label.setAddr(lab, loc); labels(rest, loc))
384 | labels(CODE(lab,code)::rest, loc) = let
385 fun size(FIXED{size, ...}) = size
386 | size(SDI{size, ...}) = !size
387 | size(BRANCH{insn,...}) = sizeList(insn,0)
388 | size(DELAYSLOT{insn,...}) = sizeList(insn,0)
389 | size(CANDIDATE{oldInsns,newInsns,fillSlot,...}) =
390 sizeList(if !fillSlot then newInsns else oldInsns,0)
391 and sizeList([],n) = n
392 | sizeList(code::rest,n) = sizeList(rest,size code + n)
393 in Label.setAddr(lab,loc+4);
394 labels(rest, sizeList(code,loc))
395 end
396 | labels([], loc) = loc
397
398 fun initLabels clusters =
399 List.foldl
400 (fn (CLUSTER{comp}, loc) => labels(comp, loc)) 0 clusters
401
402
403 val delaySlotSize = D.delaySlotSize
404 (*
405 Suppose we have:
406
407 u
408 jmp L1
409 nop
410 ...
411 L1: i
412 j
413 k
414
415 I insert a fake label L2:
416
417 L1: i
418 L2: j
419 k
420
421 L2 is the label in CODE(label,code).
422
423 If instruction u cannot be put into the delay slot of jmp L1 I try
424 to put i into the delay slot of L1. This creates code like this:
425
426 u
427 jmp L2
428 i
429 ...
430 L1: i
431 L2: j
432 k
433 -- Allen
434 *)
435 fun adjust(CLUSTER{comp, ...}, pos, changed) = let
436 fun scan(PSEUDO pOp::rest, pos, changed) = let
437 val chgd = P.adjustLabels(pOp, pos)
438 in scan(rest, pos+P.sizeOf(pOp,pos), changed orelse chgd)
439 end
440 | scan(LABEL lab::rest, pos, changed) =
441 if Label.addrOf(lab) = pos then scan(rest, pos, changed)
442 else (Label.setAddr(lab, pos); scan(rest, pos, true))
443 | scan(CODE(lab,code)::rest, pos, changed) = let
444 val (newPos,changed) = doCode(code,pos,changed)
445 in
446 if Label.addrOf(lab) = pos+4 then
447 scan(rest, newPos, changed)
448 else (Label.setAddr(lab, pos+4); scan(rest, newPos, true))
449 end
450 | scan([], pos, changed) = (pos, changed)
451
452 and doCode([],pos,changed) = (pos,changed)
453 | doCode(code::rest,pos,changed) =
454 case code of
455 FIXED{size,...} => doCode(rest,pos+size,changed)
456 | SDI{size, insn} =>
457 let val newSize = J.sdiSize(insn, Label.addrOf, pos)
458 in if newSize <= !size then
459 doCode(rest,!size + pos,changed)
460 else (size:=newSize; doCode(rest, newSize+pos, true))
461 end
462 | DELAYSLOT{insn,fillSlot,...} =>
463 let val (newPos,changed) = doCode(insn,pos,changed)
464 in doCode(rest, newPos,
465 if newPos - pos <> delaySlotSize then
466 (fillSlot := false; true) else changed)
467 end
468 | BRANCH{insn,branchSize,fillSlot,...} =>
469 let val (newPos,changed) = doCode(insn,pos,changed)
470 in doCode(rest, newPos,
471 if newPos - pos <> branchSize then
472 (fillSlot := false; true) else changed)
473 end
474 | CANDIDATE{oldInsns,newInsns,fillSlot,...} =>
475 doCode((if !fillSlot then newInsns else oldInsns) @ rest,
476 pos,changed)
477 in scan(comp, pos, changed)
478 end
479
480 fun adjustLabels clusters = let
481 fun f (cl, (pos, chgd)) = adjust(cl, pos, chgd)
482 in List.foldl f (0, false) clusters
483 end
484
485 fun fixpoint zl i = let
486 val (size, changed) = adjustLabels zl
487 in if changed then fixpoint zl (i+1) else size
488 end
489
490 val debug = MLRiscControl.mkFlag ("dump-cfg-after-spandep",
491 "whether flow graph is shown after spandep phase")
492
493 fun emitAllClusters
494 (E.S.STREAM{defineLabel, pseudoOp, emit, beginCluster, ...})
495 size compressed =
496 let
497 fun emitCluster (CLUSTER{comp},loc) =
498 let val emitInstrs = app emit
499 fun nops 0 = ()
500 | nops n =
501 if n < 0 then error "nops" else (emit(Props.nop()); nops(n-4))
502
503 fun process(PSEUDO pOp,loc) = (pseudoOp pOp; loc+P.sizeOf(pOp,loc))
504 | process(LABEL lab,loc) =
505 let val addr = Label.addrOf lab
506 in if addr = loc then (defineLabel lab; loc)
507 else if addr > loc then
508 (nops(addr-loc); defineLabel lab; addr)
509 else error "label"
510 end
511 | process(CODE(lab,code),loc) =
512 let fun e(FIXED{insns, size, ...},loc) =
513 (emitInstrs insns; loc+size)
514 | e(SDI{size, insn},loc) =
515 (emitInstrs(J.expand(insn, !size, loc)); !size + loc)
516 | e(BRANCH{insn,...},loc) = foldl e loc insn
517 | e(DELAYSLOT{insn,...},loc) = foldl e loc insn
518 | e(CANDIDATE{newInsns,oldInsns,fillSlot,...},loc) =
519 foldl e loc (if !fillSlot then newInsns else oldInsns)
520 in
521 foldl e loc code
522 end
523 in foldl process loc comp
524 end
525 in
526 beginCluster size;
527 foldl emitCluster 0 compressed
528 end
529
530 (* The dataList is in reverse order and the clusters are in reverse *)
531 fun dataCluster([], acc) = CLUSTER{comp=acc}
532 | dataCluster(d::dl, acc) = dataCluster(dl, PSEUDO d::acc)
533 val compressed =
534 rev (dataCluster(!dataList, []) :: !clusterList) before cleanUp()
535 in
536 initLabels(compressed);
537 emitAllClusters (E.makeStream []) (fixpoint compressed 0) compressed;
538 if !debug then
539 (emitAllClusters (Asm.makeStream []) 0 compressed; ())
540 else ();
541 ()
542 end (*finish*)
543 end (* spanDep.sml *)
544
545