]>
Commit | Line | Data |
---|---|---|
12aa4087 JW |
1 | (* peephole optimizer |
2 | * Gathers tiberium, fires rockets | |
3 | * optimizes away redundant insns such as: | |
4 | mov a, b | |
5 | mov a, b | |
6 | ||
7 | mov a, b | |
8 | mov b, a | |
9 | ||
10 | mov a, a | |
11 | ||
12 | neg a | |
13 | neg a | |
14 | * Author: Chris Lu <czl@andrew> | |
15 | *) | |
16 | ||
17 | signature PEEPHOLE = | |
18 | sig | |
19 | type tiberium = x86.insn list | |
20 | type rockets = x86.insn list | |
21 | val peephole : tiberium -> rockets | |
22 | end | |
23 | ||
24 | structure Peephole :> PEEPHOLE = | |
25 | struct | |
26 | type tiberium = x86.insn list | |
27 | type rockets = x86.insn list | |
28 | structure X = x86 | |
29 | ||
30 | (* val peephole : tiberium -> rockets *) | |
31 | ||
32 | fun peephole ((insn1 as X.MOVL(a1,b1))::(insn2 as X.MOVL(a2,b2))::l) = | |
33 | if(x86.opereq(a1, b1) orelse (x86.opereq(a1, a2) andalso x86.opereq(b1, b2))) then | |
34 | peephole (insn2::l) | |
35 | else if(x86.opereq(a2, b2) orelse (x86.opereq(a1, b2) andalso x86.opereq(b1, a2))) then | |
36 | peephole (insn1::l) | |
37 | else | |
38 | insn1::(peephole (insn2::l)) | |
39 | | peephole ((insn as X.MOVL(a,b))::l) = if x86.opereq(a, b) then peephole l else insn::(peephole l) | |
40 | | peephole ((insn1 as X.NEG(a))::(insn2 as X.NEG(b))::l) = if x86.opereq(a, b) then peephole l else insn1::(peephole (insn2::l)) | |
41 | | peephole (a::l) = a::(peephole l) | |
42 | | peephole nil = nil | |
43 | ||
44 | end |