-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavailable_expressions.py
164 lines (137 loc) · 4.3 KB
/
available_expressions.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
import sys
import json
import click
from copy import deepcopy
from collections import OrderedDict
from cfg import form_cfg, form_blocks, form_block_dict
from bril_core_constants import *
from worklist_solver import Worklist
AVAILABLE_GENERATORS = [
ADD, SUB, MUL, DIV,
LE, GE, LT, GT, EQ,
NOT, AND, OR
]
def instr_to_expr(instr):
assert type(instr) == dict
if OP in instr:
op = instr[OP]
if op in AVAILABLE_GENERATORS:
return tuple([op, *instr[ARGS]])
raise RuntimeError(f"Cannot Convert Instr {instr} to tuple expression.")
def expr_to_str(expr):
assert type(expr) == tuple
assert len(expr) >= 2
op = expr[0]
if op == ADD:
return f"({expr[1]} + {expr[2]})"
elif op == SUB:
return f"({expr[1]} - {expr[2]})"
elif op == MUL:
return f"({expr[1]} * {expr[2]})"
elif op == DIV:
return f"({expr[1]} // {expr[2]})"
elif op == EQ:
return f"({expr[1]} == {expr[2]})"
elif op == LE:
return f"({expr[1]} <= {expr[2]})"
elif op == GE:
return f"({expr[1]} >= {expr[2]})"
elif op == LT:
return f"({expr[1]} < {expr[2]})"
elif op == GT:
return f"({expr[1]} > {expr[2]})"
elif op == NOT:
return f"(not {expr[1]})"
elif op == AND:
return f"({expr[1]} && {expr[2]})"
elif op == OR:
return f"({expr[1]} || {expr[2]})"
raise RuntimeError(f"Cannot Handle Conversion of {expr} to String.")
def kills(instr):
assert type(instr) == dict
kills = set()
if DEST in instr:
kills.add(instr[DEST])
return kills
def gens(instr):
assert type(instr) == dict
gens = set()
if OP in instr and instr[OP] in AVAILABLE_GENERATORS:
available_expr = instr_to_expr(instr)
gens.add(available_expr)
return gens
def diff(union, kills):
assert type(union) == set
assert type(kills) == set
out = set()
for expr in union:
args = expr[1:]
add_expr = True
for k in kills:
if k in args:
add_expr = False
if add_expr:
out.add(expr)
return out
def transfer(in_block, block):
assert type(in_block) == set
assert type(block) == list
intermediate = deepcopy(in_block)
for instr_pair in block:
intermediate = diff(intermediate.union(
gens(instr_pair)), kills(instr_pair))
return intermediate
def merge(blocks):
assert type(blocks) == list
if len(blocks) == 0:
return set()
merged = set(blocks[0])
for b in blocks:
merged = merged.intersection(set(b))
return merged
def available_exprs_func(function):
cfg = form_cfg(function)
assert len(cfg) != 0
entry = list(cfg.items())[0][0]
blocks = form_block_dict(form_blocks(function["instrs"]))
init = set()
worklist = Worklist(entry, cfg, blocks, init, merge, transfer)
return worklist.solve()
def available_exprs(program):
for func in program["functions"]:
(in_dict, out_dict) = available_exprs_func(func)
final_in_dict = OrderedDict()
for (key, inner_set) in in_dict.items():
inner_lst = sorted(list(inner_set))
final_in_dict[key] = inner_lst
final_out_dict = OrderedDict()
for (key, inner_set) in out_dict.items():
inner_lst = sorted(list(inner_set))
final_out_dict[key] = inner_lst
print(f"Function: {func[NAME]}")
print(f"In:")
for (k, v) in final_in_dict.items():
if v == []:
print(f"\t{k}: No Available Expressions.")
else:
for expr in v:
print(
f"\t{k}: {expr_to_str(expr)} is available.")
print(f"Out:")
for (k, v) in final_out_dict.items():
if v == []:
print(f"\t{k}: No Available Expressions.")
else:
for expr in v:
print(
f"\t{k}: {expr_to_str(expr)} is available.")
return
@click.command()
@click.option('--pretty-print', default=False, help='Pretty Print Original Program.')
def main(pretty_print):
prog = json.load(sys.stdin)
if pretty_print:
print(json.dumps(prog, indent=4, sort_keys=True))
available_exprs(prog)
if __name__ == "__main__":
main()