-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdominance_analysis.py
276 lines (210 loc) · 8.78 KB
/
dominance_analysis.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
"""
Utilities to find dominators and the likes
"""
import sys
import json
import copy
from typing import TypeAlias, Callable
from collections import defaultdict
import click
from typing_bril import Program
from basic_blocks import (
BasicBlockProgram,
basic_block_program_from_program,
)
from cfg import (
ControlFlowGraph,
control_flow_graph_from_instructions,
reverse_cfg,
all_paths,
)
from bril_labeler import index_to_label_dict_get
DominanceAnalysis: TypeAlias = dict[int, set[int]]
def dominators_indices_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index to sets of dominators of the block index"""
doms: DominanceAnalysis = defaultdict(set)
for vertex in cfg:
doms[vertex] = set(cfg.keys())
doms[cfg.entry] = {cfg.entry}
all_indices = set([cfg.entry, cfg.exit] + list(cfg.keys()))
changing = True
while changing:
changing = False
for vertex in cfg:
if vertex == cfg.entry:
continue
intersection = all_indices.copy()
for predecessor in cfg.predecessors(vertex):
intersection &= doms[predecessor]
new_dominator_indices = {vertex} | intersection
if doms[vertex] != new_dominator_indices:
changing = True
doms[vertex] = new_dominator_indices
return doms
def naive_dominators_indices_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index to sets of dominators of the block index"""
all_indices = set([cfg.entry, cfg.exit] + list(cfg.keys()))
doms: DominanceAnalysis = {i: all_indices.copy() for i in cfg}
for i in cfg:
for path in all_paths(cfg, cfg.entry, i):
doms[i] &= set(path)
return doms
def strict_dominators_indices_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index
to sets of strict dominators of the block index"""
strict_dominators: DominanceAnalysis = {}
dominators = dominators_indices_get(cfg)
for i in cfg:
strict_dominators[i] = dominators[i] - {i}
return strict_dominators
def naive_strict_dominators_indices_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index
to sets of strict dominators of the block index"""
all_indices = set([cfg.entry, cfg.exit] + list(cfg.keys()))
sdoms: DominanceAnalysis = {i: all_indices.copy() - {i} for i in cfg}
for i in cfg:
for path in all_paths(cfg, cfg.entry, i):
for _, vertex in enumerate(path):
sdoms[vertex] &= set(path) - {vertex}
return sdoms
def immediate_dominator_index_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index
to sets of immediate dominators of the block index"""
idom: DominanceAnalysis = {}
strict_dominators = strict_dominators_indices_get(cfg)
for i in cfg:
idom[i] = strict_dominators[i].copy()
for node in strict_dominators[i]:
idom[i] -= strict_dominators[node]
if len(idom[i]) > 1:
# immediate dominator is not well-defined
# set to nil
idom[i].clear()
return idom
def naive_immediate_dominator_index_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index
to sets of immediate dominators of the block index"""
strict_dominators = naive_strict_dominators_indices_get(cfg)
idom: DominanceAnalysis = copy.deepcopy(strict_dominators)
for i in cfg:
for path in all_paths(cfg, cfg.entry, i):
for _, vertex in enumerate(path):
idom[vertex] &= set(path)
idom[vertex] -= {vertex} # strict domination
for i in cfg:
for node in strict_dominators[i]:
# idom[i] does not strictly dominate
# any other node that strictly dominates vertex
idom[i] -= strict_dominators[node]
if len(idom[i]) > 1:
# immediate dominator is not well-defined
# set to nil
idom[i].clear()
return idom
def index_dominator_tree_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dominator tree in form of a dictionary of block index
to its immediate dominator index"""
dominator_tree: DominanceAnalysis = {i: set() for i in cfg}
immediate_dominators = immediate_dominator_index_get(cfg)
for i, idoms in immediate_dominators.items():
if len(idoms) == 1:
dominator_tree[idoms.pop()].add(i)
return dominator_tree
def naive_index_dominator_tree_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dominator tree in form of a dictionary of block index
to its immediate dominator index"""
dominator_tree: DominanceAnalysis = {i: set() for i in cfg}
immediate_dominators = naive_immediate_dominator_index_get(cfg)
for i, idoms in immediate_dominators.items():
assert (
len(idoms) <= 1
), f"block index {i} has more than one immediate dominator: {idoms}"
if len(idoms) == 1:
dominator_tree[idoms.pop()].add(i)
return dominator_tree
def dominance_frontier_indices_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index
to sets of dominance frontiers of the block index"""
frontier: DominanceAnalysis = {}
dominators = dominators_indices_get(cfg)
for i in cfg:
frontier[i] = set()
for j in cfg:
predecessors = set().union(*[dominators[k] for k in cfg.predecessors(j)])
if i in predecessors and i not in dominators[j]:
# i dominates a predecessor of frontier
# i does not dominate frontier
frontier[i].add(j)
return frontier
def naive_dominance_frontier_indices_get(cfg: ControlFlowGraph) -> DominanceAnalysis:
"""Given CFG, return dictionary mapping block index
to sets of dominance frontiers of the block index"""
frontier: DominanceAnalysis = {}
dominators = naive_dominators_indices_get(cfg)
for i in cfg:
frontier[i] = set()
for j in cfg:
predecessors = set().union(*[dominators[k] for k in cfg.predecessors(j)])
if i in predecessors and i not in dominators[j]:
# i dominates a predecessor of frontier
# i does not dominate frontier
frontier[i].add(j)
return frontier
DOMINANCE_ANALYSIS_MAP: dict[str, Callable[[ControlFlowGraph], DominanceAnalysis]] = {
# dominance
"dominance": dominators_indices_get,
"naive-dominance": naive_dominators_indices_get,
# strict dominance
"strict-dominance": strict_dominators_indices_get,
"naive-strict-dominance": naive_strict_dominators_indices_get,
# immediate dominance
"immediate-dominance": immediate_dominator_index_get,
"naive-immediate-dominance": naive_immediate_dominator_index_get,
# dominator tree
"dominator-tree": index_dominator_tree_get,
"naive-dominator-tree": naive_index_dominator_tree_get,
# dominance frontier
"dominance-frontier": dominance_frontier_indices_get,
"naive-dominance-frontier": naive_dominance_frontier_indices_get,
}
@click.command()
@click.argument(
"dominance-analysis-type",
type=click.Choice(DOMINANCE_ANALYSIS_MAP.keys(), case_sensitive=False),
)
@click.option(
"-p",
"--post",
is_flag=True,
default=False,
type=bool,
help="Same analysis but with post-dominance instead of dominance",
)
def main(dominance_analysis_type: str, post: bool):
analysis = DOMINANCE_ANALYSIS_MAP[dominance_analysis_type]
prog: Program = json.load(sys.stdin)
bb_program: BasicBlockProgram = basic_block_program_from_program(prog)
analysis_result: dict[str, dict[str, list[str]]] = defaultdict(dict)
for func in bb_program["functions"]:
cfg = control_flow_graph_from_instructions(func["instrs"])
if post:
# post-dominance analysis is the same as dominance analysis
# just with cfg edges reversed
cfg = reverse_cfg(cfg)
hallucinated_blocks = (cfg.entry, cfg.exit)
index_to_label = index_to_label_dict_get(func, cfg)
dominance_analysis = analysis(cfg)
for i, _ in enumerate(func["instrs"]):
dominators_list = sorted(
[
j
for j in dominance_analysis.get(i, set())
if j not in hallucinated_blocks
]
)
analysis_result[func["name"]][index_to_label[i]] = [
index_to_label[j] for j in dominators_list
]
print(json.dumps(analysis_result))
if __name__ == "__main__":
main()