]>
Commit | Line | Data |
---|---|---|
1 | (* L3 compiler | |
2 | * peephole optimizer | |
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.cmu.edu> | |
15 | *) | |
16 | ||
17 | structure Peephole :> OPTIMIZATION = | |
18 | struct | |
19 | structure X = x86 | |
20 | ||
21 | (* val peephole : x86.insn list -> x86.insn list *) | |
22 | fun peephole ((insn1 as X.MOV(d1, s1 as (X.REL a,_)))::(insn2 as X.MOV(d2, s2 as (X.REL(a2,b2,m),_)))::l) = | |
23 | if(X.opereq(a2,d1) orelse X.opereq(b2,d1)) then | |
24 | insn1::(peephole (insn2::l)) | |
25 | else if(X.opereq(s1,s2) andalso X.opereq (d1,d2)) then | |
26 | peephole (insn2::l) | |
27 | else | |
28 | insn1::(peephole (insn2::l)) | |
29 | | peephole ((insn1 as X.MOV(a1,b1))::(insn2 as X.MOV(a2,b2))::l) = | |
30 | if(X.opereq(a1, b1) orelse (X.opereq(a1, a2) andalso X.opereq(b1, b2))) then | |
31 | peephole (insn2::l) | |
32 | else if(X.opereq(a2, b2) orelse (X.opereq(a1, b2) andalso X.opereq(b1, a2))) then | |
33 | peephole (insn1::l) | |
34 | else | |
35 | insn1::(peephole (insn2::l)) | |
36 | | peephole (X.MOV (a as (X.REG r,s), (X.CONST 0w0,_))::l) = (X.XOR (a, a))::(peephole l) | |
37 | | peephole ((insn as X.MOV(a,b))::l) = if X.opereq(a, b) then peephole l else insn::(peephole l) | |
38 | | peephole ((insn1 as X.NEG(a))::(insn2 as X.NEG(b))::l) = if X.opereq(a, b) then peephole l else insn1::(peephole (insn2::l)) | |
39 | | peephole (X.ADD (_, (X.CONST 0w0,_))::l) = peephole l | |
40 | | peephole (X.SUB (_, (X.CONST 0w0,_))::l) = peephole l | |
41 | (* | peephole (X.CMP ((X.REG r,s), (X.CONST 0w0,_))::l) = (X.TEST ((X.REG r,s), (X.REG r,s)))::(peephole l) *) | |
42 | | peephole ((X.JMP a)::(X.JMP b)::l) = peephole ((X.JMP a)::l) (* What the cock? Yes, we actually generate this. *) | |
43 | | 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) | |
44 | | peephole (a::l) = a::(peephole l) | |
45 | | peephole nil = nil | |
46 | ||
47 | val optimizer = { shortname = "peephole", description = "Peephole analysis", func = Optimizer.FINAL peephole } | |
48 | end |