-
Notifications
You must be signed in to change notification settings - Fork 287
Expand file tree
/
Copy pathremove_unused_functions.cpp
More file actions
82 lines (66 loc) · 2.05 KB
/
remove_unused_functions.cpp
File metadata and controls
82 lines (66 loc) · 2.05 KB
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
/*******************************************************************\
Module: Unused function removal
Author: CM Wintersteiger
\*******************************************************************/
/// \file
/// Unused function removal
#include "remove_unused_functions.h"
#include <util/message.h>
void remove_unused_functions(
goto_functionst &functions,
message_handlert &message_handler)
{
std::set<irep_idt> used_functions;
std::list<goto_functionst::function_mapt::iterator> unused_functions;
find_used_functions(
goto_functionst::entry_point(), functions, used_functions);
for(goto_functionst::function_mapt::iterator it=
functions.function_map.begin();
it!=functions.function_map.end();
it++)
{
if(used_functions.find(it->first)==used_functions.end())
unused_functions.push_back(it);
}
messaget message(message_handler);
if(!unused_functions.empty())
{
message.statistics()
<< "Dropping " << unused_functions.size() << " of " <<
functions.function_map.size() << " functions (" <<
used_functions.size() << " used)" << messaget::eom;
}
for(const auto &f : unused_functions)
functions.function_map.erase(f);
}
void find_used_functions(
const irep_idt &start,
goto_functionst &functions,
std::set<irep_idt> &seen)
{
std::pair<std::set<irep_idt>::const_iterator, bool> res =
seen.insert(start);
if(!res.second)
return;
else
{
goto_functionst::function_mapt::const_iterator f_it =
functions.function_map.find(start);
if(f_it!=functions.function_map.end())
{
forall_goto_program_instructions(it, f_it->second.body)
{
if(it->type==FUNCTION_CALL)
{
const code_function_callt &call =
to_code_function_call(to_code(it->code));
// check that this is actually a simple call
assert(call.function().id()==ID_symbol);
find_used_functions(call.function().get(ID_identifier),
functions,
seen);
}
}
}
}
}