-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
optimization_pass.py
83 lines (58 loc) · 2.11 KB
/
optimization_pass.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
import ailment
from ...analysis import Analysis
class OptimizationPass(Analysis):
ARCHES = [ ] # strings of supported architectures
PLATFORMS = [ ] # strings of supported platforms. Can be one of the following: "win32", "linux"
def __init__(self, func, blocks=None):
self._func = func
self._blocks = blocks
@property
def blocks(self):
return self._blocks
def analyze(self):
ret, cache = self._check()
if ret:
self._analyze(cache=cache)
def _check(self):
"""
Check if this optimization applies to this function.
:returns: a tuple of (does_apply, cache) where cache is a way to pass
information to _analyze so it does not have to be recalculated
"""
raise NotImplementedError()
def _analyze(self, cache=None):
"""
Run the analysis.
:param cache: information passed from _check so it does not have to be
recalculated
:returns: None
"""
raise NotImplementedError()
#
# Util methods
#
def _get_block(self, addr, size=None):
original_block = self._func.get_node(addr)
if original_block is None:
# this block does not exist
return None
if size is None:
size = original_block.size
if not self._blocks:
return original_block
else:
return self._blocks.get((addr, size), original_block)
def _update_block(self, old_block, new_block):
addr, size = old_block.addr, old_block.original_size
if self._blocks is None:
self._blocks = { }
self._blocks[(addr, size)] = new_block
def _remove_block(self, block):
addr, size = block.addr, block.original_size
if self._blocks is None:
self._blocks = { }
self._blocks[(addr, size)] = None
def _is_add(self, expr):
return isinstance(expr, ailment.Expr.BinaryOp) and expr.op == "Add"
def _is_sub(self, expr):
return isinstance(expr, ailment.Expr.BinaryOp) and expr.op == "Sub"