]>
Commit | Line | Data |
---|---|---|
6ade8b0a | 1 | (* L3 compiler |
0a24e44d | 2 | * peephole optimizer |
12aa4087 JW |
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 | |
0a24e44d | 14 | * Author: Chris Lu <czl@andrew.cmu.edu> |
12aa4087 JW |
15 | *) |
16 | ||
17 | signature PEEPHOLE = | |
18 | sig | |
0a24e44d | 19 | val peephole : x86.insn list -> x86.insn list |
12aa4087 JW |
20 | end |
21 | ||
22 | structure Peephole :> PEEPHOLE = | |
23 | struct | |
12aa4087 JW |
24 | structure X = x86 |
25 | ||
0a24e44d | 26 | (* val peephole : x86.insn list -> x86.insn list *) |
12aa4087 | 27 | |
6ade8b0a | 28 | fun peephole ((insn1 as X.MOV(a1,b1))::(insn2 as X.MOV(a2,b2))::l) = |
12aa4087 JW |
29 | if(x86.opereq(a1, b1) orelse (x86.opereq(a1, a2) andalso x86.opereq(b1, b2))) then |
30 | peephole (insn2::l) | |
31 | else if(x86.opereq(a2, b2) orelse (x86.opereq(a1, b2) andalso x86.opereq(b1, a2))) then | |
32 | peephole (insn1::l) | |
33 | else | |
34 | insn1::(peephole (insn2::l)) | |
6ade8b0a JW |
35 | | peephole (X.MOV (X.REG r, X.CONST 0w0)::l) = (X.XOR (X.REG r, X.REG r))::(peephole l) |
36 | | peephole ((insn as X.MOV(a,b))::l) = if x86.opereq(a, b) then peephole l else insn::(peephole l) | |
12aa4087 | 37 | | 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)) |
6ade8b0a JW |
38 | | peephole (X.ADD (_, X.CONST 0w0)::l) = peephole l |
39 | | peephole (X.SUB (_, X.CONST 0w0)::l) = peephole l | |
40 | | peephole (X.CMP (X.REG r, X.CONST 0w0)::l) = (X.TEST (X.REG r, X.REG r))::(peephole l) | |
41 | | peephole ((X.JMP a)::(X.JMP b)::l) = peephole ((X.JMP a)::l) (* What the cock? Yes, we actually generate this. *) | |
42 | | peephole ((X.JMP l1)::(X.LABEL l2)::l) = if (Label.compare (l1,l2) = EQUAL) then (X.LABEL l2)::(peephole l) else (X.JMP l1)::(X.LABEL l2)::(peephole l) | |
43 | | peephole (X.SIZE (s, i)::l) = map (fn i => X.SIZE (s, i)) (peephole [i]) @ (peephole l) (* :/ that kind of sucks, but oh well *) | |
12aa4087 JW |
44 | | peephole (a::l) = a::(peephole l) |
45 | | peephole nil = nil | |
46 | ||
47 | end |