-
Notifications
You must be signed in to change notification settings - Fork 22
/
peephole.rb
277 lines (247 loc) · 5.98 KB
/
peephole.rb
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
# # Very basic peephole optimizer
#
# This is *not* intended to be a general purpose peephole optimizer
# It's a quick and dirty hack to encapsulate optimizations that can
# be made quickly on the asm that is harder to fix in the higher level
# code generation. It is allowed to make assumptions about the generated
# code which may not hold for asm generated by anything else.
#
# This is i386 specific at the moment
class Peephole
def initialize out
@out = out
@prev = []
end
def comment(str)
#flush
#@out.comment(str)
end
def export(label, type = nil)
flush
@out.export(label, type)
end
def label(l)
flush
@out.label(l)
end
def match(pat, test)
return nil if pat.length != test.length
res = []
pat.each_with_index do |c,i|
if c === test[i]
res << test[i]
else
return nil
end
end
return res
end
# Other patterns:
# pushl %eax
# movl ?, %eax
# movl %eax, otherreg
# popl %eax
# ->
# movl ?, reg
# subl n, %esp
# ... x ops not touching %esp
# addl n, %esp
# ->
# ... x ops not touching %esp
# *especially improving __int and __get_symbol init
# Alt is handling the addl n, %esp/single instr not touching esp/subl n, %esp
#
# movl -n(%ebp), %eax
# movl %eax, reg
# popl %eax
# movl %eax, (reg)
# ->
# movl -n(%ebp), reg
# popl %eax
# movl %eax, (reg)
#
#
# subl x, %esp
# op ?, reg
# subl y, %esp
# ->
# subl x+y, %esp
# op ?, reg
#
#
# This *can't* be optimal:
# movl n, %eax
# cmpl %eax, %eax
# setne %al
# movzbl %al, %eax
# testl %eax, %eax
# je label
#
# Surely:
# cmpl n, %eax
# je label
# must be enough?
#
#
# movl n(%ebp), %esi
# movl n(%ebp), %eax
# must be a bug?
#
# movl -n(%ebp), %edx
# pushl %edx
# movl m(%ebp), %esi
# popl %edi
# movl %edi, o(%esi)
# ->
#movl m(%ebp), %esi
#movl -n(%ebp), %edx
#movl %edx, o(%esi)
def peephole
return if @prev.empty?
args = @prev[-1]
last = @prev[-2]
# subl $0, reg
if match([:subl, 0, Symbol], args)
@prev.pop
return
end
if args == [:movl, :eax, :eax]
@prev.pop
return
end
if last
if last == [:pushl, :ebx] && args == [:movl, "-4(%ebp)", :eax]
@prev.pop
@prev << [:movl, :ebx, :eax]
return
end
#if match([:movl, String, :eax], last) &&
# match([:movl, :eax, String], args)
# @prev.pop
# @prev.pop
# @prev << [:movl, last[1], args[2]]
# return
#end
if last == [:pushl, :eax] &&
match([:popl, Symbol], args)
# @unsafe Only ok because we know the compiler treats %eax as scratch
@prev.pop
@prev.pop
@prev << [:movl, :eax, args[1]]
return
end
if match([:movl, Integer, :eax], last) &&
match([:cmpl, :eax, Symbol], args)
# @unsafe this optimization is ok only because we know the compiler treats
# %eax as a scratch register
@prev.pop
@prev.pop
@prev << [:cmpl, last[1], args[2]]
return
end
if last[0] == :pushl && args[0] == :popl && last[1] == args[1]
@prev.pop
@prev.pop
return
end
if args[0] == :subl &&
last[1].is_a?(Integer) &&
args[1].is_a?(Integer) && last[2] == args[2]
if last[0] == :addl &&
# addl x, dest
# subl y, dest
@prev.pop
@prev.pop
if last[1] > args[1]
@prev << [:addl, last[1] - args[1], args[2]]
return
elsif last[1] < args[1]
@prev << [:subl, args[1] - last[1], args[2]]
end
return
end
if last[0] == :subl
# subl x, dest
# subl y, dest
@prev.pop
@prev.pop
@prev << [:subl, args[1] + last[1], args[2]]
return
end
end
last2 = @prev[-3]
if last2
if last2[0] == :movl
if last2[2] == :eax
# movl ???, %eax
if last2[1].class == Symbol
src = last2[1]
if last[0] == :movl && last[1] == :eax
# movl reg, %eax
# movl %eax, dest
dest = last[2]
if args[0] == :movl && args[2] == :eax
#movl reg, %eax
#movl %eax, dest
#movl ???, %eax
#->
#movl reg, dest
#movl ???, %eax
@prev.pop
@prev.pop
@prev.pop
@prev << [:movl, src, dest]
@prev << args
return
end
end
end
if last2[1].class == Symbol || last2[1].is_a?(Integer)
val = last2[1]
# movl Int|Reg, %eax
if last[0] == :subl
if last[1] == :eax
if last[2].class == Symbol
reg = last[2]
# movl $2, %eax
# subl %eax, reg
if args[0] == :movl
if args[1] != :eax
if args[2] == :eax
# movl $2, %eax
# subl %eax, reg
# movl ???, %eax
@prev.pop
@prev.pop
@prev.pop
@prev << [:subl, val, reg]
@prev << args
return
end
end
end
end
end
end
end
end
end
end
end
end
def emit(*args)
@prev << args
l = @prev.length
while l > 0
peephole
return if @prev.length >= l
l = @prev.length
end
end
def flush
@prev.each do |row|
@out.emit(*row)
end
@prev = []
end
end