-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstant_propagation.py
160 lines (136 loc) · 5.43 KB
/
constant_propagation.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
import json
import sys
import fileinput
from brilpy import *
# Worklist for constant folding
def constant_prop(fnc, g):
inp = [].append({})
out = [].append({})
if "args" in fnc:
for arg in fnc["args"]:
inp[0][arg["name"]] = None
for x in range(g.n - 1):
inp.append({})
out.append({})
return (inp, out)
def transfer(inp, block):
out = {}
for key, val in inp.items():
out[key] = val
# [Intra-procedural analysis](https://www.cs.cmu.edu/~aldrich/courses/15-819O-13sp/resources/interprocedural.pdf)
for instr in block:
if "op" in instr and "dest" in instr and instr["type"] == "int":
if instr["op"] == "const":
out[instr["dest"]] = instr["value"]
elif instr["op"] == "call":
out[instr["dest"]] = None
else:
if len(instr["args"]) == 1:
if instr["args"][0] in out and instr["args"][0] != None:
out[instr["dest"]] = out[instr["args"][0]]
else:
out[instr["dest"]] = None
else:
if (
instr["args"][0] in out
and out[instr["args"][0]] != None
and instr["args"][1] in out
and out[instr["args"][1]] != None
):
int_operations = {
"add": lambda x, y: x + y,
"sub": lambda x, y: x - y,
"mul": lambda x, y: x * y,
"div": lambda x, y: int(x / y),
}
out[instr["dest"]] = int_operations[instr["op"]](
out[instr["args"][0]], out[instr["args"][1]]
)
else:
out[instr["dest"]] = None
elif "op" in instr and "dest" in instr and instr["type"] == "float":
if instr["op"] == "const":
out[instr["dest"]] = instr["value"]
elif instr["op"] == "call":
out[instr["dest"]] = None
else:
if len(instr["args"]) == 1:
if instr["args"][0] in out and instr["args"][0] != None:
out[instr["dest"]] = out[instr["args"][0]]
else:
out[instr["dest"]] = None
else:
if (
instr["args"][0] in out
and out[instr["args"][0]] != None
and instr["args"][1] in out
and out[instr["args"][1]] != None
):
float_operations = {
"fadd": lambda x, y: x + y,
"fsub": lambda x, y: x - y,
"fmul": lambda x, y: x * y,
"fdiv": lambda x, y: x / y,
}
out[instr["dest"]] = float_operations[instr["op"]](
out[instr["args"][0]], out[instr["args"][1]]
)
else:
out[instr["dest"]] = None
elif "op" in instr and "dest" in instr and instr["type"] == "bool":
if instr["op"] == "const":
out[instr["dest"]] = instr["value"]
elif instr["op"] == "call":
out[instr["dest"]] = None
else:
if len(instr["args"]) == 1:
if instr["args"][0] in out and instr["args"][0] != None:
out[instr["dest"]] = out[instr["args"][0]]
else:
out[instr["dest"]] = None
else:
if (
instr["args"][0] in out
and out[instr["args"][0]] != None
and instr["args"][1] in out
and out[instr["args"][1]] != None
):
bool_operations = {
"flt": lambda x, y: x < y,
"fgt": lambda x, y: x > y,
"feq": lambda x, y: x == y,
"lt": lambda x, y: x < y,
"gt": lambda x, y: x > y,
"eq": lambda x, y: x == y,
}
out[instr["dest"]] = bool_operations[instr["op"]](
out[instr["args"][0]], out[instr["args"][1]]
)
else:
out[instr["dest"]] = None
return out
def merge(map_lst):
result = {}
var = set()
for m in map_lst:
var = var | p.keys()
for v in var:
val = [p[v] for m in map_lst if v in m]
if val.count(val[0]) == len(val) and None not in val:
result[v] = val[0]
else:
result[v] = None
return result
def main():
file = ""
for line in fileinput.input():
file += line
file = json.loads(file)
for func in file["functions"]:
print(f"{func['name']}")
cfg = CFG(func)
(inp, out) = run_worklist(func, rd_init, rd_xfer, rd_merge)
for i, name in enumerate(cfg.names):
print(f"{name}:\n {inp[i]} (in)\n {out[i]} (out)\n\n")
if __name__ == "__main__":
main()