Debian Codegraph @

lib/mlrisc-lib/MLRISC/gc-safety/gc-liveness.sml

raw
1 (*
2 * Compute typed liveness information for garbage collection safety
3 *)
4 functor GCLiveness
5 (structure IR : MLRISC_IR
6 structure GC : GC_TYPE
7 structure InsnProps : INSN_PROPERTIES
8 sharing InsnProps.I = IR.I
9 ) : GC_LIVENESS =
10 struct
11
12 structure IR = IR
13 structure C = IR.I.C
14 structure CFG = IR.CFG
15 structure GC = GC
16 structure G = Graph
17 structure GCTypeMap = GCTypeMap(GC)
18 structure R = GCTypeMap
19 structure A = Array
20
21 structure Liveness =
22 Dataflow
23 (structure CFG = CFG
24 type domain = R.typemap
25 val forward = false
26 val bot = R.empty
27 val == = R.==
28 val join = R.joins
29 type dataflow_info =
30 (C.cell -> GC.gctype) *
31 { liveIn : R.typemap, liveOut : R.typemap } A.array
32 fun mk(gcmap,regs) =
33 R.fromList(map (fn r => (r,gcmap r)) regs)
34
35 fun liveOut(gcmap,b as CFG.BLOCK{id,...}) =
36 let val cellset = CFG.liveOut(b)
37 val regs = C.CellSet.toCellList cellset
38 in mk(gcmap,regs)
39 end
40
41 val defUseR = InsnProps.defUse C.GP
42 val defUseF = InsnProps.defUse C.FP
43
44 fun scan(gcmap,CFG.BLOCK{insns,...}) =
45 let fun loop([],def,use) = (def,use)
46 | loop(i::is,def,use) =
47 let val (d1,u1) = defUseR i
48 val (d2,u2) = defUseF i
49 val d = mk(gcmap,d1 @ d2)
50 val u = mk(gcmap,u1 @ u2)
51 (* val _ = print("d="^R.toString d^" ")
52 val _ = print("u="^R.toString u^"\n")
53 val _ = print("use-d="^R.toString(R.kill(use,d))^"\n")*)
54 val use = R.gen(R.kill(use,d),u)
55 val def = R.kill(R.gen(def,d),u)
56 (*val _ = print("def="^R.toString def^" ")
57 val _ = print("use="^R.toString use^"\n") *)
58 in loop(is,def,use)
59 end
60 in loop(!insns,R.empty,R.empty) end
61
62 fun prologue (_,(gcmap,_)) (b,b') =
63 let val (def,use) = scan(gcmap,b')
64 val liveOut = liveOut(gcmap,b')
65 in (* print("Liveout("^Int.toString b^")="^R.toString liveOut^"\n");
66 print("def("^Int.toString b^")="^R.toString def^"\n");
67 print("use("^Int.toString b^")="^R.toString use^"\n"); *)
68 { input = liveOut,
69 output = R.gen(R.kill(liveOut,def),use),
70 transfer = fn liveOut => R.gen(R.kill(liveOut,def),use)
71 }
72 end
73 fun epilogue (_,(_,table))
74 {node=(b,_), input=liveOut, output=liveIn } =
75 ((* print("Livein("^Int.toString b^")="^R.toString liveIn^"\n");
76 print("Liveout("^Int.toString b^")="^R.toString liveOut^"\n");*)
77 A.update(table,b,{liveIn=liveIn,liveOut=liveOut})
78 )
79 )
80
81 fun liveness (IR as G.GRAPH cfg) =
82 let val an = !(CFG.annotations IR)
83 val table = A.array(#capacity cfg (),{liveIn=R.empty,liveOut=R.empty})
84 fun gclookup(C.CELL{an, ...}) = #lookup GC.GC_TYPE (!an)
85 val _ = Liveness.analyze(IR,(gclookup,table))
86 in table
87 end
88
89 end