-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopopt.sml
423 lines (372 loc) · 11.4 KB
/
loopopt.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
signature LOOPOPT =
sig
val optimize: X86.funcode -> X86.funcode
end
structure LoopOpt : LOOPOPT = struct
structure M = X86
structure RS = M.RegSet
fun compareBlock (x,y) =
let val (lab1,insts1) = x
val (lab2,insts2) = y
in
Symbol.compare (lab1, lab2)
end
fun isEqualBlock (x,y) =
case compareBlock(x,y) of
EQUAL => true
| _ => false
fun print_set (set:RS.set) : unit =
(RS.app (fn reg => (print " "; print (M.reg2name reg))) set; print "\n")
structure BBSet = RedBlackSetFn(type ord_key = M.codeblock
val compare = compareBlock)
fun list2set l = BBSet.addList(BBSet.empty,l)
structure CFG = Graph(BBSet)
fun labToBlock (h::t) lab =
let val (l,instrs) = h in
if (l = lab) then h else labToBlock t lab
end
| labToBlock [] lab = ErrorMsg.impossible "No label found"
fun nextLabel funCode (lab:M.lab) =
let fun first_label_of ((ret, _)::t) = (ret) | first_label_of [] = Symbol.symbol "#exit" in
case funCode of
(lab1, _)::t => if (lab1 = lab) then first_label_of t else nextLabel t lab
| [] => Symbol.symbol "#exit"
end
fun nextBlock funCode (lab:M.lab) =
let val nextlab = (nextLabel funCode lab)
in
if Symbol.name nextlab = "#exit" then (nextlab, [])
else
labToBlock funCode (nextLabel funCode lab)
end
exception EmptyBlock
fun exitOf (lab, h::t) = hd(rev(h::t))
| exitOf (lab, []) = raise EmptyBlock
fun makeCFG cfg funCode =
let
val _ = List.map (fn x => (CFG.succ cfg x;x)) (funCode)
fun calcSucc block =
let
fun addSucc to =
CFG.mk_edge cfg {from=block, to=to}
val lastinst = exitOf block
val (thisLab, thisInstrs) = block
in
case lastinst of
M.J(lab) => addSucc (labToBlock funCode lab)
| M.Branchz(_,_,lab) => (addSucc (labToBlock funCode lab); addSucc (nextBlock funCode thisLab))
| M.Branch(_,_,_,lab) => (addSucc (labToBlock funCode lab); addSucc (nextBlock funCode thisLab))
| _ => addSucc (nextBlock funCode thisLab)
end
val _ = List.map (calcSucc) funCode
in
()
end
(* DomMap *)
structure DomMap = RedBlackMapFn( struct type ord_key = M.codeblock val compare = compareBlock end)
fun bbset2string (set:BBSet.set) =
BBSet.foldl (fn ((lab,instrs),str) => (str^" "^(Symbol.name lab))) "" set
fun bb2string (block:M.codeblock) =
let val (lab, _) = block
in
Symbol.name lab
end
fun dumpDomMap map =
let
val blockList = DomMap.listItemsi map
fun dumpEntries ((key,set)::t) =
let
val (lab, instrs) = key
in
print (Symbol.name lab^" <= "^bbset2string set^"\n");
dumpEntries t
end
| dumpEntries [] = ()
in
dumpEntries blockList
end
fun isEqualDomMap (map1, map2) =
let
fun compareOneInTheSecond (key, set)=
case DomMap.find(map2, key) of
NONE => false
| SOME s => BBSet.equal( s, set )
val list1 = DomMap.listItemsi map1
fun compareAllInTheFirst (h::t) = (compareOneInTheSecond h) andalso compareAllInTheFirst t
| compareAllInTheFirst [] = true
in
compareAllInTheFirst list1
end
fun makeDomMap cfg entry =
let
val initialDomMap = BBSet.foldl (fn (b,m) => DomMap.insert(m, b, CFG.nodes cfg)) DomMap.empty (CFG.nodes cfg)
fun updateDom oldmap (block, map) =
let
val (lab, _) = block
(*val _ = print ("updating :"^Symbol.name lab^"\n" )*)
fun dom block =
if isEqualBlock (block, entry) then BBSet.singleton(block)
else
case DomMap.find( oldmap, block ) of
NONE => BBSet.empty
| SOME v => v
fun intersect (block, set) =
BBSet.intersection( set, dom block )
in
if isEqualBlock (block, entry)
then
DomMap.insert(map, block, BBSet.singleton(block))
else
let
(*val _ = print ("predecessors: "^bbset2string (CFG.pred cfg block)^"\n")*)
val intersected = BBSet.foldl intersect (CFG.nodes cfg) (CFG.pred cfg block)
(*val _ = print ("bbset is:"^bbset2string intersected^"\n")*)
in
DomMap.insert( map, block, BBSet.union(intersected, BBSet.singleton(block)))
end
end
fun updateDomAll oldDomMap =
let
val newDomMap = BBSet.foldl (updateDom oldDomMap) DomMap.empty (CFG.nodes cfg)
in
if isEqualDomMap(oldDomMap, newDomMap)
then
(*(print "\n *** DOMMAP: \n"; dumpDomMap newDomMap;newDomMap)*)newDomMap
else
(*(print ("notEqual\n\n\n\n\n\n");*)
updateDomAll newDomMap
end
in
updateDomAll initialDomMap
end
fun printsucc say g (i,insts) =
(say (Symbol.name i); say ":";
CFG.S.app (fn (j,_) => (say " "; say (Symbol.name j))) (CFG.succ g (i,insts));
say "\n")
fun printgraph say g = CFG.S.app (printsucc say g) (CFG.nodes g);
fun findLoops cfg map =
let
fun doDFS (marked:BBSet.set) (to:M.codeblock) (from:M.codeblock, set:BBSet.set):BBSet.set =
let
(*val _ = print ("doDFS in "^bb2string from^"\n")*)
val (fromLab, _) = from
val (toLab, _) = to
in
if fromLab = toLab orelse (BBSet.member (marked, from))
then
BBSet.add(set,from)
else
BBSet.union(
BBSet.foldl
(doDFS (BBSet.add(marked, from)) to)
(BBSet.empty)
(CFG.pred cfg from),
BBSet.add (set, from) )
end
fun findloop (block:M.codeblock,looplist) =
let
(*val _ = print (bb2string block)*)
val (lab,_) = block
val SOME dominators = DomMap.find( map, block )
val backto = BBSet.intersection(dominators , (CFG.succ cfg block))
(*val _ = print (" header: "^bbset2string backto^"\n")*)
in
case BBSet.numItems backto of
0 => looplist
| 1 => let
val [header] = BBSet.listItems backto
val body = doDFS (BBSet.empty) header (block, BBSet.empty)
(*val _ = print ("*** Found Loop : header("^bb2string header^") and body("^bbset2string body^")\n")*)
in
(header,body)::looplist
end
| _ => ErrorMsg.impossible "'Double back edge from one node' case is not implemented!"
end
in
BBSet.foldl (findloop) [] (CFG.nodes cfg)
end
fun defsInBody (body:BBSet.set):RS.set =
let
fun defsInInstruction (instruction, set:RS.set) =
RS.union( set, #def (M.instr_def_use instruction))
fun defsInBlock (block:M.codeblock, set:RS.set):RS.set =
let val (lab, instrs) = block
in
foldl (defsInInstruction) set instrs
end
in
BBSet.foldl (defsInBlock) RS.empty body
end
fun invariantsInBody (alldefs:RS.set) (invariants:RS.set) (body:BBSet.set):RS.set =
let
fun isInvariant (instruction:M.instruction) =
let
fun useRegInvariant (r:M.reg, isit:bool) =
(
isit
andalso
(
RS.member(invariants,r)
orelse
(not (RS.member(alldefs,r)))
)
)
val uses = #use (M.instr_def_use instruction)
val isUseRegsInvariant = RS.foldl useRegInvariant true uses
in
case instruction of
M.Arith1 (aop1,_) => isUseRegsInvariant
| M.Arith2 (aop2,_,_) => isUseRegsInvariant
| M.Arith3 (aop3,_,_,_) => isUseRegsInvariant
| M.Arithi (aopi,_,_, immed) => isUseRegsInvariant
| M.Li (_, immed) => true
| M.La (_, lab) => true
| M.Lw (_, address) => false
| M.Sw (_, address) => false
| M.Move (_,_) => isUseRegsInvariant
| M.Branchz (comparecode1,_, lab) => false
| M.Branch (comparecode1,_,_, lab) => false
| M.J (lab) => false
| M.Jal (lab) => false
| M.Jr (_,_) => false
| M.Jalr (_,_, _, _) => false
| M.Nop => false
| M.Syscall => false
| M.Leave => false
| M.Ret => false
| M.Push (_) => false
| M.Pop (_) => false
| M.Branch2 (comparecode1, lab) => false
end
fun invariantsInInstruction (instruction, set:RS.set) =
let val defs = #def (M.instr_def_use instruction)
in
if (isInvariant instruction)
then
RS.union(set, defs)
else
set
end
fun invariantsInBlock ((lab, instrs), set:RS.set) =
foldl (invariantsInInstruction) set instrs
val oldNumInvariants = RS.numItems invariants
val newInvariants = BBSet.foldl invariantsInBlock invariants body
val newNumInvariants = RS.numItems newInvariants
in
if (oldNumInvariants = newNumInvariants)
then
newInvariants
else
invariantsInBody alldefs newInvariants body
end
(*
fun filterConstraints (invariants:RS.set) (body:BBSet.set):RS.set =
let
fun filterInBlock (block:M.codeblock,filtered) =
in
body
end
*)
fun singleDefsInBody (body:BBSet.set):RS.set =
let
fun singleDefsInInstruction (instruction, (set:RS.set,mset:RS.set)):RS.set * RS.set =
let val defs = #def (M.instr_def_use instruction)
(*val _ = print (M.instr2string instruction)
val _ = print_set defs
val _ = print "set is "
val _ = print_set (RS.union(set,defs))
val _ = print "mset is "
val _ = print_set (RS.union(RS.intersection(defs, set) , mset) )*)
in
(RS.union(set,defs), ( RS.union(RS.intersection(defs, set) , mset) ))
end
fun singleDefsInBlock (block:M.codeblock, (set:RS.set,mset:RS.set)):RS.set *RS.set =
let val (lab, instrs) = block
in
foldl (singleDefsInInstruction) (set, mset) instrs
end
in
let val (set,mset) = BBSet.foldl (singleDefsInBlock) (RS.empty, RS.empty) body
in
RS.difference(set, mset)
end
end
fun addPreheaderToFunCode (h::t) header preheaderInsts =
let
(*val _ = print "addPreheaderToFunCode\n"*)
val (headerLab,_) = header
val (thisLab,_) = h
in
if (headerLab = thisLab)
then
(M.freshlab(), preheaderInsts)::(h::t)
else
h::(addPreheaderToFunCode t header preheaderInsts)
end
| addPreheaderToFunCode [] _ _ = []
fun optimize (funCode:M.funcode) : M.funcode =
let
fun optimizeLoop ((header:M.codeblock, body:BBSet.set), code:M.funcode) =
let
fun isInTheLoop (block:M.codeblock):bool =
BBSet.member(body, block)
fun removeInvariantInsts (invariants:RS.set) (block:M.codeblock):M.codeblock =
let
val (lab, thisInstrs) = block
in
if isInTheLoop( block )
then
(lab, List.filter (fn (inst:M.instruction) =>
let val defs = #def (M.instr_def_use inst) in
if RS.numItems (RS.intersection(invariants, defs)) > 0 then false else true end)
thisInstrs)
else
block
end
fun extractInvariantInsts (invariants:RS.set) (block:M.codeblock, instrs:M.instruction list)=
let
val (lab, thisInstrs) = block
in
if isInTheLoop( block )
then
foldl (fn (inst,l) =>
let val defs = #def (M.instr_def_use inst) in
if RS.numItems (RS.intersection(invariants, defs)) > 0
then
inst::l
else
l
end
) instrs thisInstrs
else
instrs
end
val (headerLab,_) = header
val SOME liveoutOfPreheader = Symbol.look(
Liveness.analyze
{mention=(fn (x:M.reg) => ()),interfere=(fn (x:M.reg) => ( fn (y:M.reg) => ()))}
funCode,
headerLab )
val defset = defsInBody (body)
val singledefs = singleDefsInBody (body)
val invariants = RS.difference(
RS.intersection( invariantsInBody defset RS.empty (body), singledefs),
liveoutOfPreheader )
val preheaderInsts = foldl (extractInvariantInsts invariants) [] code
val funCodeAfterRemoval = List.map (removeInvariantInsts invariants) code
val newFunCode = addPreheaderToFunCode funCodeAfterRemoval header preheaderInsts
in
newFunCode
end
val g = CFG.newGraph()
val _ = makeCFG g funCode
val domMap = makeDomMap g (hd(funCode))
val loops = findLoops g domMap
(*val _ = optimizeLoops loops*)
(*val _ = print ("###### DomMap\n")*)
in
(*printgraph print g;
print ("###### END \n");*)
foldl optimizeLoop funCode loops
end
end