Debian Codegraph @

lib/mlrisc-lib/MLRISC/SSA/ssa-ccp-basis.sml

raw
1 (* Conditional constant propagation.
2 * This module performs the conditional constant propagation analysis,
3 * but does not update the SSA graph.
4 *
5 * -- Allen ([email protected])
6 *)
7
8 functor SSACondConstProp(CF : SSA_CONSTANT_FOLDING)
9 : SSA_CONDITIONAL_CONSTANT_PROPAGATION =
10 struct
11
12 structure CF = CF
13 structure SSA = CF.SSA
14 structure RTL = SSA.RTL
15 structure T = RTL.T
16 structure G = Graph
17 structure A = Array
18 structure W8A = Word8Array
19
20 type valueMap = CF.valnum Array.array
21
22 fun error msg = MLRiscErrorMsg.error("SSACondConstProp",msg)
23
24 fun condConstProp(SSA as G.GRAPH ssa) =
25 let val CFG as G.GRAPH cfg = SSA.cfg SSA
26 val N = #capacity cfg ()
27 val M = #capacity ssa ()
28 val {ops, phis, sources, ...} = SSA.nodes SSA
29 val defSiteTbl = SSA.defSiteTbl SSA
30 val usesTbl = SSA.usesTbl SSA
31 val defsTbl = SSA.defsTbl SSA
32 val rtlTbl = SSA.rtlTbl SSA
33 val blockTbl = SSA.blockTbl SSA
34 val reachable = W8A.array(N,0w0) (* blocks that are reachable *)
35 val onWorkList = W8A.array(M,0w0) (* ssa ops that are on work list *)
36 val bot = CF.bot
37 val top = CF.top
38 val nodes = SSA.nodes SSA
39 val V = SSA.maxVar SSA
40 val values = A.array(V,bot) (* current values *)
41
42 (* Add value v onto the worklist *)
43 fun addWL(v,WL) =
44 let val i = A.sub(defSiteTbl,v)
45 in if W8A.sub(onWorkList,i) <> 0w0 then WL
46 else (W8A.update(onWorkList,i,0w1); i::WL)
47 end
48
49 (* Add all operations onto the worklist *)
50 fun addWLs([],WL) = WL
51 | addWLs(i::ops,WL) =
52 (W8A.update(onWorkList,i,0w1); addWLs(ops, i::WL))
53
54 (* Constant folding function *)
55 val fold = CF.constantFolding SSA (fn _ => top)
56
57 (* Perform propagation on the worklist *)
58 fun propagate [] = ()
59 | propagate(i::WL) =
60 (W8A.update(onWorkList,i,0w0);
61 propagate(eval(i,A.sub(rtlTbl,i),
62 A.sub(defsTbl,i),A.sub(usesTbl,i),WL)))
63
64 (* Evaluate an SSA node *)
65 and eval(i,T.PHI _,[t],s,WL) =
66 let fun join([],v) = v
67 | join(s::ss,v) =
68 let val x = A.sub(values,s)
69 in if x = bot then join(ss,v)
70 else if v = bot then join(ss,x)
71 else if x = v then join(ss,v)
72 else top
73 end
74 in update(t,join(s,bot),WL)
75 end
76 | eval(i,T.SOURCE _,t,_,WL) = updateTops(t,WL)
77 | eval(i,T.SINK _,_,_,WL) = WL
78 | eval(i,i',s,t,WL) =
79 let fun getVal v = if v >= 0 then A.sub(values, v) else v
80 val v = fold(i',map getVal s,0,())
81 val b = A.sub(blockTbl,i)
82 in case t of
83 [t] => let val WL = update(t,v,WL)
84 fun chase e =
85 case (e,v) of
86 (T.IF _,~1) => enableSucc(b,false,WL)
87 | (T.IF _,~2) => enableSucc(b,true,WL)
88 | (T.IF _,_) => enableAllSucc(b,WL)
89 | (T.JMP _,_) => enableAllSucc(b,WL)
90 | (T.RTL{e,...},_) => chase e
91 | _ => WL
92 in chase i'
93 end
94 | _ => updateTops(t,WL)
95 end
96
97 (* Update the result *)
98 and update(t,v,WL) =
99 if A.sub(values,t) = v then WL
100 else (A.update(values,t,v); addWL(t,WL))
101
102 (* Update top to all results *)
103 and updateTops([],WL) = WL
104 | updateTops(t::ts,WL) =
105 if A.sub(values,t) = top then updateTops(ts,WL)
106 else (A.update(values,t,top); updateTops(ts,addWL(t,WL)))
107
108 (* Mark a basic block as reachable *)
109 and enableNode(b,WL) =
110 if W8A.sub(reachable,b) <> 0w0 then WL
111 else
112 let val _ = W8A.update(reachable,b,0w1)
113 val WL = addWLs(A.sub(ops,b), WL)
114 val WL = addWLs(A.sub(phis,b), WL)
115 val WL = addWLs(A.sub(sources,b), WL)
116 in case #out_edges cfg b of
117 [(_,b',_)] => enableNode(b',WL)
118 | _ => WL
119 end
120
121 (* Mark an edge as reachable *)
122 and enableEdge((_,b,_),WL) = enableNode(b,WL)
123
124 (* Mark a successor of b as reachable *)
125 and enableSucc(b,cond,WL) =
126 foldr (fn ((_,b',_),WL) => enableNode(b',WL)) WL (#out_edges cfg b)
127
128 (* Mark all successors of b as reachable *)
129 and enableAllSucc(b,WL) = foldr enableEdge WL (#out_edges cfg b)
130
131 (* Initialize the table *)
132 fun init() =
133 (case SSA.C.zeroReg SSA.C.GP of
134 SOME zeroR => A.update(values, zeroR, CF.zero)
135 | NONE => ()
136 )
137
138 (* Constant propagation main driver *)
139 fun constantPropagation() =
140 (init();
141 propagate(enableAllSucc(hd(#entries cfg ()),[]))
142 )
143 in constantPropagation();
144 values
145 end
146
147 end