-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlvn.py
295 lines (277 loc) · 10.2 KB
/
lvn.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Local Variable Numbering Pass
bril2json < *.bril | python lvn.py | bril2txt
bril2json < *.bril | python lvn.py | brili -p # profile dynamic instr count
"""
import sys
import json
from basic_block import form_basic_blocks
def str2bool(boolean):
if boolean:
return 'true'
else:
return 'false'
def compute(instr, env, tuples, table):
"""Try to compute the instruction,
If computable, return a constant instr,
else return the original instr.
"""
# skip instr without dest
# nothing to compute for them
if 'dest' not in instr or 'op' not in instr:
return instr
# Build value tuple
all_const_operands = False
any_const_operands = False
args_identical = False
if 'args' in instr:
if all(item in env for item in instr['args']):
if instr['op'] == 'id':
# let const propagation handle id
# we skip them here
return instr
arg_nums = [env[arg_name] for arg_name in instr['args']]
arg_nums.sort()
# Are all operands constant?
const_operands = [tuples[num][0] == 'const' for num in arg_nums]
all_const_operands = all(const_operands)
any_const_operands = any(const_operands)
else: # func args
arg_nums = instr['args']
# Are operands identical?
args_identical = len(set(arg_nums)) == 1
# Any operands constant?
local_args = [env[arg_name] for arg_name in instr['args'] if arg_name in env]
any_const_operands = any([tuples[num][0] == 'const' for num in local_args])
else: # const instr
return instr
# print(instr)
# print(const_operands)
# print(any_const_operands)
# print("\n")
op = instr['op']
const_instr = {'op':'const', 'dest':instr['dest']}
if all_const_operands: # let's do computation!
# first, get those const args
args = [tuples[num][1] for num in arg_nums]
if op == 'ne':
value = str2bool(args[0] != args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'eq':
value = str2bool(args[0] == args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'le':
value = str2bool(args[0] <= args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'lt':
value = str2bool(args[0] < args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'gt':
value = str2bool(args[0] > args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'ge':
value = str2bool(args[0] >= args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'not':
value = str2bool(not args[0])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'and':
value = str2bool(args[0] and args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'or':
value = str2bool(args[0] or args[1])
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'add':
value = args[0] + args[1]
const_instr['value'] = value
const_instr['type'] = 'int'
return const_instr
elif op == 'mul':
value = args[0] * args[1]
const_instr['value'] = value
const_instr['type'] = 'int'
return const_instr
elif op == 'sub':
value = args[0] - args[1]
const_instr['value'] = value
const_instr['type'] = 'int'
return const_instr
elif op == 'div':
value = args[0] / args[1]
const_instr['value'] = value
const_instr['type'] = 'int'
return const_instr
elif any_const_operands:
if op == 'and': # and false any = true
for arg_name in instr['args']:
if arg_name not in env: continue
value = tuples[env[arg_name]][1]
if value == False:
value = str2bool(False)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'or': # or true any = false
for arg_name in instr['args']:
if arg_name not in env: continue
value = tuples[env[arg_name]][1]
if value == True:
value = str2bool(True)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif args_identical:
if op == 'ne':
value = str2bool(False)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'eq':
value = str2bool(True)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'le':
value = str2bool(True)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'lt':
value = str2bool(False)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'gt':
value = str2bool(False)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
elif op == 'ge':
value = str2bool(True)
const_instr['value'] = value
const_instr['type'] = 'bool'
return const_instr
return instr
class unique(object):
"""Provides unique value id
"""
num = 0
def __init__(self):
pass
@classmethod
def increase(cls):
cls.num += 1
@classmethod
def get(cls):
return cls.num
def is_overwritten(dest, instrs):
for instr in instrs:
if 'dest' not in instr: continue
if instr['dest'] == dest:
return True
return False
def lvn(block, debug=False):
new_block = list()
# env: symbol name -> local value number
env = dict() # str -> int
# local value numbering table
table = dict() # int -> {'value_tuple': tuple, 'cname': str}
# list of value tuples for book-keeping
tuples = list()
for idx, instr in enumerate(block):
old_name = None
# try compute the instr for constant folding
instr = compute(instr, env, tuples, table)
# skip the labels
if 'op' not in instr: continue
# Build value tuple
if 'args' in instr:
if all(item in env for item in instr['args']):
arg_nums = [env[arg_name] for arg_name in instr['args']]
arg_nums.sort()
value_tuple = (instr['op'], *arg_nums)
else: # func args
value_tuple = (instr['op'], *instr['args'])
else: # const instr
value_tuple = (instr['op'], instr['value'])
# re-generate the instr's argument
if 'args' in instr and all(item in env for item in instr['args']):
arg_numbers = [env[arg_name] for arg_name in instr['args']]
instr['args'] = [table[number]['cname'] for number in arg_numbers]
if value_tuple in tuples:
# if the value is in the table (computed)
# then we use the value
num = tuples.index(value_tuple)
opcode = table[num]['value_tuple'][0]
if opcode != 'const':
canonical_name = table[num]['cname']
instr['op'] = 'id'
instr['args'] = [canonical_name]
else:
instr['op'] = 'const'
instr['value'] = table[num]['value_tuple'][1]
elif instr['op'] == "id": # copying from a value
id_operand_num = value_tuple[1] # copying from which value
opcode = table[id_operand_num]['value_tuple'][0] # is it a const?
if opcode == 'const': # const propagation
instr['op'] = 'const'
instr['value'] = table[num]['value_tuple'][1]
else:
# add an entry to the table
tuples.append(value_tuple)
num = len(tuples) - 1
if 'dest' in instr:
cname = instr['dest']
# overwritten case
"""e.g.
x = a + b
# use x for some time
x = c + d
y = a + b # lvn will try to replace this with y = x, which is wrong
The reason is that x as "canonical value" corresponds to both a+b and c+d
value tuples.
"""
if idx + 1 < len(block) \
and is_overwritten(instr['dest'], block[idx+1:]):
cname = "lvn." + str(unique.get())
unique.increase()
# rename the destination
old_name = instr['dest']
instr['dest'] = cname
# update the table
table[num] = {'value_tuple': value_tuple, 'cname': cname}
# update the env
if 'dest' in instr:
if old_name is not None:
env[old_name] = num
else:
env[instr['dest']] = num
new_block.append(instr)
return new_block
def main():
prog = json.load(sys.stdin)
for func in prog['functions']:
new_blocks = list()
for block in form_basic_blocks(func['instrs']):
new_blocks += lvn(block)
func['instrs'] = new_blocks
print(json.dumps(prog))
if __name__ == "__main__":
main()