Debian Codegraph @

lib/mlrisc-lib/MLRISC/amd64/instructions/amd64Peephole.peep

raw
1 (*
2 * Note, this file contains conditional pattern matching rules.
3 * You'll have to run it thru the tool wheregen
4 * (source for this is in the directory Tools/WhereGen)
5 * to generate the output.
6 *
7 * -- Allen
8 *)
9
10 local
11
12 (* ew! *)
13 structure I =
14 struct
15 include "amd64Instr.sml" (* import instruction definitions *)
16 end
17
18 in
19
20 functor AMD64Peephole
21 (structure Instr : AMD64INSTR
22 structure Eval : MLTREE_EVAL
23 (* sharing Instr.T = Eval.T *)
24 where type T.Basis.cond = Instr.T.Basis.cond
25 and type T.Basis.div_rounding_mode = Instr.T.Basis.div_rounding_mode
26 and type T.Basis.ext = Instr.T.Basis.ext
27 and type T.Basis.fcond = Instr.T.Basis.fcond
28 and type T.Basis.rounding_mode = Instr.T.Basis.rounding_mode
29 and type T.Constant.const = Instr.T.Constant.const
30 and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) Instr.T.Extension.ccx
31 and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) Instr.T.Extension.fx
32 and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) Instr.T.Extension.rx
33 and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) Instr.T.Extension.sx
34 and type T.I.div_rounding_mode = Instr.T.I.div_rounding_mode
35 and type T.Region.region = Instr.T.Region.region
36 and type T.ccexp = Instr.T.ccexp
37 and type T.fexp = Instr.T.fexp
38 (* and type T.labexp = Instr.T.labexp *)
39 and type T.mlrisc = Instr.T.mlrisc
40 and type T.oper = Instr.T.oper
41 and type T.rep = Instr.T.rep
42 and type T.rexp = Instr.T.rexp
43 and type T.stm = Instr.T.stm
44 ) : PEEPHOLE =
45 struct
46 structure I = Instr
47 structure C = I.C
48 structure CBase = CellsBasis
49
50 (* IMPORTANT: instructions are given in forward order *)
51 fun peephole instrs =
52 let fun isStackPtr(I.Direct (_, r)) = CBase.sameColor(r, C.rsp)
53 | isStackPtr _ = false
54
55 fun isZeroLE le = (Eval.valueOf le = 0) handle _ => false
56
57 fun isZero(I.Immed n) = n = 0
58 | isZero(I.ImmedLabel le) = isZeroLE le
59 | isZero _ = false
60
61 fun isZeroOpt NONE = true
62 | isZeroOpt (SOME opn) = isZero opn
63
64 fun loop(code, instrs) =
65 (case code of
66 [] => instrs
67
68 (* x <- x +/- 0;
69 *)
70 | I.INSTR(I.BINARY{binOp=(I.ADDL | I.SUBL | I.ADDQ | I.SUBQ),
71 src=I.ImmedLabel le, ...})::rest
72 where isZeroLE le => loop(rest, instrs)
73
74 (* remove lea 0(r), r *)
75 | I.INSTR(I.LEAL{r32,
76 addr=I.Displace{base, disp=I.ImmedLabel le,...}})::rest
77 where (isZeroLE le) andalso
78 CBase.sameColor(r32,base) => loop(rest, instrs)
79
80 | I.INSTR(I.LEAQ{r64,
81 addr=I.Displace{base, disp=I.ImmedLabel le,...}})::rest
82 where (isZeroLE le) andalso
83 CBase.sameColor(r64,base) => loop(rest, instrs)
84
85 (* addq n, %rsp; subq m, %rsp
86 * => addq (n-m), %rsp ;; when m < n
87 * => - ;; when m = n
88 * => subq (m-n), %rsp ;; when m > n
89 *)
90 | I.INSTR(I.BINARY{binOp=I.ADDQ, src=I.Immed n, dst=I.Direct (_, d_i)})::
91 I.INSTR(I.BINARY{binOp=I.SUBQ, src=I.Immed m, dst=I.Direct (ty, d_j)})::
92 rest
93 where CBase.sameColor(d_i, C.rsp) andalso
94 CBase.sameColor(d_j, C.rsp) =>
95 if (m = n) then loop (rest, instrs)
96 else if (m < n) then
97 loop(rest,
98 I.binary{binOp=I.ADDQ, src=I.Immed(n-m),
99 dst=I.Direct(ty, C.rsp)}::instrs)
100 else
101 loop(rest,
102 I.binary{binOp=I.SUBQ, src=I.Immed(m-n),
103 dst=I.Direct(ty, C.rsp)}::instrs)
104
105 (* push folding:
106 * subq 4, %rsp
107 * movq src, 0(%rsp) (where src <> %rsp !!! )
108 * =>
109 * pushq src
110 *)
111 | I.INSTR(I.BINARY{binOp=I.SUBQ,src=I.Immed 4,dst=I.Direct (_, dst_i)})::
112 I.INSTR(I.MOVE{mvOp=I.MOVQ,src,
113 dst=I.Displace{base,disp=I.Immed 0,...}})
114 ::rest
115 where CBase.sameColor(base, C.rsp) andalso
116 CBase.sameColor(dst_i, C.rsp) andalso
117 not(isStackPtr src) =>
118 loop(rest, I.pushq src::instrs)
119
120 (* pop folding:
121 * movq 0(%rsp), dst (where dst <> %rsp!!!!)
122 * addq 4, %rsp
123 * =>
124 * popq dst
125 *)
126 | I.INSTR(I.MOVE{mvOp=I.MOVQ,
127 src=I.Displace{base, disp=I.Immed 0, ...}, dst})::
128 I.INSTR(I.BINARY{binOp=I.ADDQ, src=I.Immed 4,
129 dst=I.Direct (_, dst_i)})::
130 rest
131 where CBase.sameColor(base, C.rsp) andalso
132 CBase.sameColor(dst_i,C.rsp) andalso
133 not(isStackPtr dst) =>
134 loop(rest, I.pop dst::instrs)
135
136 | I.INSTR(I.MOVE{mvOp=(I.MOVQ|I.MOVL), src, dst as I.Direct _})::rest
137 where isZero src =>
138 loop(rest, I.binary{binOp=I.XORL, src=dst, dst=dst}::instrs)
139
140 | i::rest => loop(rest, i::instrs)
141 )
142 in loop(instrs, [])
143 end
144 end
145
146 end