Debian Codegraph @

lib/mlrisc-lib/MLRISC/SSA/ssa-osr.sml

raw
1 (*
2 * Operator Strength Reduction.
3 *
4 * -- Allen ([email protected])
5 *)
6 functor SSAOSR(CF : SSA_CONSTANT_FOLDING) : SSA_OPTIMIZATION =
7 struct
8 structure SSA = CF.SSA
9 structure Dom = SSA.Dom
10 structure I = SSA.I
11 structure RTL = SSA.RTL
12 structure T = RTL.T
13 structure G = Graph
14 structure A = Array
15 structure DA = DynArray
16 structure H = HashTable
17
18 type flowgraph = SSA.ssa
19
20 fun error msg = MLRiscErrorMsg.error("SSAOperatorStrengthReduction",msg)
21
22 val name = "Operator Strength Reduction"
23
24 fun run(SSA as G.GRAPH ssa) = SSA
25 (*
26 let val Dom as G.GRAPH dom = SSA.dom SSA
27 val sdom = Dom.strictly_dominates Dom
28 val replaceAllUses = SSA.replaceAllUses SSA
29 val showOp = SSA.showOp SSA
30 val N = #capacity ssa ()
31 val V = SSA.maxVar SSA
32 val defSiteTbl = SSA.defSiteTbl SSA
33 val defsTbl = SSA.defsTbl SSA
34 val usesTbl = SSA.usesTbl SSA
35 val rtlTbl = SSA.rtlTbl SSA
36 val blockTbl = SSA.blockTbl SSA
37
38 val headers = DA.array(V,~1) (* headers of instructions *)
39 (* ~1 if it is not an induction variable *)
40 val inSCC = DA.array(N,~1) (* is the instruction in the SCC? *)
41
42 exception NotThere
43
44 val table = CF.hashTable(13,NotThere)
45 val search = H.lookup table
46 val insert = H.insert table
47 val inventName = I.C.newCell I.C.GP
48
49 fun add(e,operands,name) = insert((e,operands,0),name)
50
51 (* Check whether value x is a region constant *)
52 fun isRegionConstant(x,header) =
53 x < 0 orelse sdom(A.sub(blockTbl,A.sub(defSiteTbl,x)),header)
54
55 (* Copy an instruction *)
56 fun copyDef'(SSA.OP{b,i,e,s,p,...},t) = SSA.OP{b=b,i=i,e=e,s=s,p=p,t=[t]}
57 | copyDef'(SSA.PHI{b,s,t',preds,...},t) =
58 SSA.PHI{b=b,t'=t',s=s,preds=preds,t=t}
59 | copyDef' _ = error "copyDef'"
60
61 fun copyDef(x,t) =
62 let val i' = node_info x
63 val i = #new_id ssa ()
64 in #add_node ssa (i,copyDef'(i',t));
65 (i,i')
66 end
67
68 (* Process each scc *)
69 fun processSCC ([],_) = ()
70 | processSCC ([n],_) = strengthReduce(n,node_info n)
71 | processSCC (scc as witness::_,_) =
72 let (* find the header block of the SCC *)
73 fun findHeader([],scc,h) = (scc,h)
74 | findHeader(i::ops,scc,h) =
75 let val i' = node_info i
76 in DA.update(inSCC,i,witness);
77 findHeader(ops,(i,i')::scc,
78 case i' of
79 SSA.PHI{b,...} => if h = ~1 orelse sdom(b,h)
80 then b else h
81 | _ => h
82 )
83 end
84 val (scc,header) = findHeader(scc,[],~1)
85
86 (* Check whether the scc is an inductive variable *)
87 fun isIVSCC [] = true
88 | isIVSCC ((_,i')::ops) = isIVOp i' andalso isIVSCC ops
89
90 (* is the operation a legal inductive cycle? *)
91 and isIVOp(SSA.PHI{s,...}) = List.all isIVorRC s
92 | isIVOp(SSA.OP{e=T.COPY _,s=[s],...}) = isIVorRC s
93 | isIVOp(SSA.OP{e=T.RTL{e, ...}, s=[a,b], ...}) =
94 let fun f(T.MV(_,_,T.ADD(_,T.REG(_,0) ,T.REG(_,1)))) =
95 isIVRC(a,b) orelse isIVRC(b,a)
96 | f(T.MV(_,_,T.ADD(_,T.REG(_,1) ,T.REG(_,0)))) =
97 isIVRC(a,b) orelse isIVRC(b,a)
98 | f(T.MV(_,_,T.ADDT(_,T.REG(_,0) ,T.REG(_,1)))) =
99 isIVRC(a,b) orelse isIVRC(b,a)
100 | f(T.MV(_,_,T.ADDT(_,T.REG(_,1) ,T.REG(_,0)))) =
101 isIVRC(a,b) orelse isIVRC(b,a)
102 | f(T.MV(_,_,T.SUB(_,T.REG(_,0),T.REG(_,1)))) =
103 isIVRC(a,b)
104 | f(T.MV(_,_,T.SUB(_,T.REG(_,1),T.REG(_,0)))) =
105 isIVRC(b,a)
106 | f(T.MV(_,_,T.SUBT(_,T.REG(_,0),T.REG(_,1)))) =
107 isIVRC(a,b)
108 | f(T.MV(_,_,T.SUBT(_,T.REG(_,1),T.REG(_,0)))) =
109 isIVRC(b,a)
110 | f _ = false
111 in f e end
112 | isIVOp _ = false
113 and isIV x = x >= 0 andalso DA.sub(inSCC,x) = witness
114 and isRC x = isRegionConstant(x,header)
115 and isIVRC(a,b) = isIV a andalso isRC b
116 and isIVorRC x = isIV x orelse isRC x
117
118 fun dumpSCC(title,scc) =
119 (print(title^"="^Int.toString header^"\n");
120 app (fn (_,i) => print("\t"^showOp i^"\n")) scc)
121
122 in if isIVSCC scc then
123 (* found an induction variable *)
124 let fun mark t = DA.update(headers,t,header)
125 in dumpSCC("IV",scc);
126 app (fn (_,SSA.OP{t,...}) => app mark t
127 | (_,SSA.PHI{t,...}) => mark t
128 | _ => error "headers") scc
129 end
130 else
131 (app strengthReduce scc)
132 end
133
134 (* perform strength reduction *)
135 and strengthReduce(n,n' as SSA.OP{e,t=[t],s=[a,b],...}) =
136 (case isInReducibleForm(e,a,b) of
137 SOME(iv,rc) => replace(t,e,iv,rc)
138 | NONE => ())
139 | strengthReduce _ = ()
140
141 (* Check whether an instruction is in reducible form *)
142 and isInReducibleForm(rtl,a,b) =
143 let fun isIVRC(a,b) =
144 a >= 0 andalso
145 let val header_a = DA.sub(headers,a)
146 in header_a <> ~1 andalso isRegionConstant(b,header_a)
147 end
148 fun addOrMul() =
149 if isIVRC(a,b) then SOME(a,b)
150 else if isIVRC(b,a) then SOME(b,a)
151 else NONE
152 fun sub() =
153 if isIVRC(a,b) then SOME(a,b) else NONE
154 fun reducible rtl =
155 case rtl of
156 T.MV(_,_,T.ADD(_,T.REG(_,0),T.REG(_,1))) => addOrMul()
157 | T.MV(_,_,T.ADDT(_,T.REG(_,0),T.REG(_,1))) => addOrMul()
158 | T.MV(_,_,T.MULS(_,T.REG(_,0),T.REG(_,1))) => addOrMul()
159 | T.MV(_,_,T.MULU(_,T.REG(_,0),T.REG(_,1))) => addOrMul()
160 | T.MV(_,_,T.MULT(_,T.REG(_,0),T.REG(_,1))) => addOrMul()
161 | T.MV(_,_,T.SUB(_,T.REG(_,0),T.REG(_,1))) => sub()
162 | T.MV(_,_,T.SUBT(_,T.REG(_,0),T.REG(_,1))) => sub()
163 | T.RTL{e, ...} => reducible e
164 | _ => NONE
165 in reducible rtl
166 end
167
168 (*
169 * Replace the current operation with a copy from its
170 * reduced counterpart.
171 *)
172 and replace(t,e,iv,rc) =
173 let val t' = reduce(e,iv,rc)
174 in replaceAllUses{from=t,to=t'};
175 DA.update(headers,t,DA.sub(headers,iv))
176 end
177
178 (*
179 * Insert code to strength reduce an induction variable
180 * and return the SSA name of the result
181 *)
182 and reduce(e,iv,rc) =
183 let val operands = [iv,rc]
184 in search(e,operands,0)
185 handle _ =>
186 let val result = inventName()
187 val _ = add(e,operands,result)
188 val (newDef,_) = copyDef(iv,result)
189 val iv_header = DA.sub(headers,iv)
190 val _ = DA.update(headers,result,iv_header)
191 fun isMult(T.RTL{e, ...}) = isMult e
192 | isMult(T.MV(_,_,T.MULS(_,T.REG(_,0),T.REG(_,1)))) = true
193 | isMult(T.MV(_,_,T.MULU(_,T.REG(_,0),T.REG(_,1)))) = true
194 | isMult(T.MV(_,_,T.MULT(_,T.REG(_,0),T.REG(_,1)))) = true
195 | isMult _ = false
196 fun doOperand r =
197 if r >= 0 andalso DA.sub(headers,r) = iv_header then
198 (replaceAllUses{from=r,to=reduce(e,r,rc)};())
199 else if r >= 0 orelse isMult e andalso
200 (case node_info (defSite r) of
201 SSA.PHI _ => true
202 | _ => false) then
203 (replaceAllUses{from=r,to=apply(e,r,rc)}; ())
204 else ()
205 in app doOperand operands;
206 result
207 end
208 end
209
210 and apply(e,op1,op2) =
211 let val operands = [op1,op2]
212 in search(e,operands,0)
213 handle _ =>
214 if op1 < 0 orelse
215 let val header_op1 = DA.sub(headers,op1)
216 in header_op1 <> ~1 andalso
217 isRegionConstant(op2,header_op1)
218 end then
219 reduce(e,op1,op2)
220 else if op2 < 0 orelse
221 let val header_op2 = DA.sub(headers,op2)
222 in header_op2 <> ~1 andalso
223 isRegionConstant(op1,header_op2)
224 end then
225 reduce(e,op2,op1)
226 else
227 let val result = inventName()
228 val _ = add(e,operands,result)
229 in result
230 end
231 end
232
233 in (* process all loops *)
234 GraphSCC.strong_components(ReversedGraphView.rev_view SSA) processSCC ();
235 SSA.changed SSA;
236 SSA
237 end
238 *)
239
240 end