forked from sampsyo/bril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivial_dce.py
36 lines (32 loc) · 1.06 KB
/
trivial_dce.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
import sys
import json
from form_blocks import form_blocks
def trivial_dce(func):
blocks = list(form_blocks(func['instrs']))
converged = True
for block in blocks:
leftout_insts = {}
remove_set = set()
for i, inst in enumerate(block):
if 'args' in inst:
for arg in inst['args']:
if arg in leftout_insts:
del leftout_insts[arg]
if 'dest' in inst:
if inst['dest'] in leftout_insts:
remove_set.add(inst['dest'])
del leftout_insts[inst['dest']]
leftout_insts[inst['dest']] = i
for k,v in leftout_insts.items():
remove_set.add(v)
if len(remove_set) == 0:
converged = False
for i, inst in enumerate(block):
if i in remove_set:
block.remove(inst)
return converged
if __name__ == '__main__':
program = json.load(sys.stdin)
for func in program["functions"]:
while not trivial_dce(func):
pass