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
|
#!/usr/bin/env python
'''
LLVM doesn't appear to have a way to remove unused functions. This little
script will do that. It requires annotations to be in the .ll file it parses
(run llvm-dis with -show-annotations).
Closure compiler can remove unused functions, however it is much faster
to remove them before Emscripten runs.
'''
import os, sys, re
def path_from_root(*pathelems):
rootpath = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
return os.path.join(rootpath, *pathelems)
exec(open(path_from_root('tools', 'shared.py'), 'r').read())
infile = sys.argv[1]
outfile = sys.argv[2]
lines = open(infile, 'r').read().split('\n')
class Dummy: pass
# Discover functions
functions = {}
func_header = re.compile('^define[^@]* (?P<ident>@\w+)\(.* {$')
func_footer = '}'
func_annot = re.compile('^; \[#uses=(?P<uses>\d+)\]$')
print '\nDiscovery pass 1\n'
for i in range(len(lines)):
line = lines[i]
m_header = func_header.match(line)
if m_header:
m_annot = func_annot.match(lines[i-1])
assert m_annot
ident = m_header.group('ident')
func = functions[ident] = Dummy()
func.uses = int(m_annot.group('uses')) # XXX This info from LLVM is very inaccurate
func.callers = set()
func.callees = set()
for ident in functions.iterkeys():
func = functions[ident]
#print ident
if '@main' not in functions:
print 'No @main found, not running DFE'
import shutil
shutil.copy(infile, outfile)
sys.exit(1)
print '\nDiscovery pass 2\n'
ident_frag = re.compile('[, ](?P<ident>@\w+)[, ()}\]]')
metadata = re.compile('!(?P<index>\d+) = metadata !{.*')
inside = None
for i in range(len(lines)):
line = lines[i]
if line == func_footer:
inside = None
continue
m_header = func_header.match(line)
if m_header:
inside = m_header.group('ident')
continue
meta = metadata.match(line)
for m in re.finditer(ident_frag, line):
ident = m.groups('ident')[0]
if ident not in functions: continue
if inside != ident:
functions[ident].callers.add(inside if inside else ('GLOBAL' if not meta else 'METADATA_'+str(i)+'_'+meta.groups('index')[0]))
if inside:
functions[inside].callees.add(ident)
functions['@main'].callers.add('GLOBAL')
for ident in functions.iterkeys():
func = functions[ident]
#print ident, func.uses, func.callers#, 'WARNING!' if func.uses != len(func.callers) else ''
# Garbage collect
print '\nGC pass 1\n'
for ident in functions.iterkeys():
func = functions[ident]
func.root = func.marked = False
for caller in func.callers:
if caller == 'GLOBAL':
func.root = True
#print 'ROOT:', ident
break
def mark_and_recurse(func):
if func.marked: return
func.marked = True
for callee in func.callees:
if callee == 'GLOBAL': continue
mark_and_recurse(functions[callee])
for ident in functions.iterkeys():
func = functions[ident]
if func.root:
mark_and_recurse(func)
marked = unmarked = 0
for ident in functions.iterkeys():
func = functions[ident]
if func.root: assert func.marked
#print ident, func.marked
marked += func.marked
unmarked += 1-func.marked
dead_metadatas = set() # metadata pruning pass
for ident in functions.iterkeys():
func = functions[ident]
if func.marked: continue
for caller in func.callers:
if caller.startswith('METADATA_'):
dummy, i, index = caller.split('_')
lines[int(i)] = ';'
dead_metadatas.add(int(index))
inner_metadata = re.compile('metadata !(?P<index>\d+)')
for i in range(len(lines)):
line = lines[i]
if metadata.match(line):
lines[i] = re.sub(inner_metadata, lambda m: 'i32 0' if int(m.groups('index')[0]) in dead_metadatas else m.string[m.start():m.end()], line)
print 'Marked: ', marked, ', unmarked: ', unmarked
# Write
print '\nWriting\n'
inside = None
marked = False
target = open(outfile, 'w')
for line in lines:
if line == func_footer:
inside = None
if marked: target.write(line + '\n')
continue
m_header = func_header.match(line)
if m_header:
inside = m_header.group('ident')
marked = functions[inside].marked
######### if metadata.match(line): continue # metadata is not enough to keep things alive
if line.startswith('!llvm.dbg.sp = '): continue
if not inside or marked:
if len(line) > 0:
target.write(line + '\n')
target.close()
|