Skip to content

Remove functions that are not reachable from the entry point #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 49 additions & 18 deletions src/analyses/call_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Author: Daniel Kroening, [email protected]

/*******************************************************************\

Function: call_grapht::call_grapht
Function: call_grapht::operator()

Inputs:

Expand All @@ -23,23 +23,7 @@ Function: call_grapht::call_grapht

\*******************************************************************/

call_grapht::call_grapht()
{
}

/*******************************************************************\

Function: call_grapht::call_grapht

Inputs:

Outputs:

Purpose:

\*******************************************************************/

call_grapht::call_grapht(const goto_functionst &goto_functions)
void call_grapht::operator()()
{
forall_goto_functions(f_it, goto_functions)
{
Expand Down Expand Up @@ -96,6 +80,53 @@ void call_grapht::add(

/*******************************************************************\

Function: call_grapht::compute_reachable

Inputs:

Outputs:

Purpose:

\*******************************************************************/

void call_grapht::compute_reachable(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is goto-programs/compute_called_functions.cpp - the code of which will also take into account functions the address of which is taken.

const irep_idt entry_point,
std::unordered_set<irep_idt, irep_id_hash> &reachable_functions)
{
assert(reachable_functions.empty());

std::list<irep_idt> worklist;

const goto_functionst::function_mapt::const_iterator e_it=
goto_functions.function_map.find(entry_point);

assert(e_it!=goto_functions.function_map.end());

worklist.push_back(entry_point);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add: reachable_functions.insert(entry_point); - reasons below.


do
{
const irep_idt id=worklist.front();
worklist.pop_front();

reachable_functions.insert(id);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this one.


const auto &p=graph.equal_range(id);

for(auto it=p.first; it!=p.second; it++)
{
const irep_idt callee=it->second;

if(reachable_functions.find(callee)==reachable_functions.end())
worklist.push_back(callee);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace by

if(reachable_functions.insert(callee).second)
  worklist.push_back(callee);

to avoid duplicate lookups.

}
}
while(!worklist.empty());
}

/*******************************************************************\

Function: call_grapht::output_dot

Inputs:
Expand Down
13 changes: 11 additions & 2 deletions src/analyses/call_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ Author: Daniel Kroening, [email protected]

#include <iosfwd>
#include <map>
#include <unordered_set>

#include <goto-programs/goto_functions.h>

class call_grapht
{
public:
call_grapht();
explicit call_grapht(const goto_functionst &);
explicit call_grapht(const goto_functionst &goto_functions) :
goto_functions(goto_functions) {}

void operator()();

void output_dot(std::ostream &out) const;
void output(std::ostream &out) const;
Expand All @@ -29,7 +32,13 @@ class call_grapht

void add(const irep_idt &caller, const irep_idt &callee);

void compute_reachable(
const irep_idt entry_point,
std::unordered_set<irep_idt, irep_id_hash> &reachable_functions);

protected:
const goto_functionst &goto_functions;

void add(const irep_idt &function,
const goto_programt &body);
};
Expand Down
35 changes: 35 additions & 0 deletions src/goto-instrument/goto_instrument_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,40 @@ void goto_instrument_parse_optionst::instrument_goto_program()
slice_global_inits(ns, goto_functions);
}

if(cmdline.isset("remove-unreachable-functions"))
{
status() << "Removing functions that are not reachable from the entry point"
<< eom;

const irep_idt &entry_point=goto_functionst::entry_point();

goto_functionst::function_mapt::const_iterator e_it;
e_it=goto_functions.function_map.find(entry_point);

if(e_it==goto_functions.function_map.end())
throw "entry point not found";

call_grapht call_graph(goto_functions);
call_graph();

std::unordered_set<irep_idt, irep_id_hash> reachable_functions;
call_graph.compute_reachable(entry_point, reachable_functions);

for(goto_functionst::function_mapt::iterator it=
goto_functions.function_map.begin();
it!=goto_functions.function_map.end();)
{
const irep_idt &id=it->first;

if(reachable_functions.find(id)==reachable_functions.end())
it=goto_functions.function_map.erase(it);
else
it++;
}

goto_functions.update();
}

if(cmdline.isset("string-abstraction"))
{
status() << "String Abstraction" << eom;
Expand Down Expand Up @@ -1538,6 +1572,7 @@ void goto_instrument_parse_optionst::help()
" --full-slice slice away instructions that don't affect assertions\n" // NOLINT(*)
" --property id slice with respect to specific property only\n" // NOLINT(*)
" --slice-global-inits slice away initializations of unused global variables\n" // NOLINT(*)
" --remove-unreachable-functions remove unreachable functions\n"
"\n"
"Further transformations:\n"
" --constant-propagator propagate constants and simplify expressions\n" // NOLINT(*)
Expand Down
1 change: 1 addition & 0 deletions src/goto-instrument/goto_instrument_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Author: Daniel Kroening, [email protected]
"(show-struct-alignment)(interval-analysis)(show-intervals)" \
"(show-uninitialized)(show-locations)" \
"(full-slice)(reachability-slice)(slice-global-inits)" \
"(remove-unreachable-functions)" \
"(inline)(partial-inline)(function-inline):(log):" \
"(remove-function-pointers)" \
"(show-claims)(show-properties)(property):" \
Expand Down
33 changes: 6 additions & 27 deletions src/goto-programs/slice_global_inits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,19 @@ void slice_global_inits(
{
// gather all functions reachable from the entry point

call_grapht call_graph(goto_functions);
const call_grapht::grapht &graph=call_graph.graph;

std::list<irep_idt> worklist;
std::unordered_set<irep_idt, irep_id_hash> functions_reached;

const irep_idt entry_point=goto_functionst::entry_point();

goto_functionst::function_mapt::const_iterator e_it;
e_it=goto_functions.function_map.find(entry_point);
const goto_functionst::function_mapt::const_iterator e_it=
goto_functions.function_map.find(entry_point);

if(e_it==goto_functions.function_map.end())
throw "entry point not found";

worklist.push_back(entry_point);

do
{
const irep_idt id=worklist.front();
worklist.pop_front();

functions_reached.insert(id);

const auto &p=graph.equal_range(id);

for(auto it=p.first; it!=p.second; it++)
{
const irep_idt callee=it->second;
call_grapht call_graph(goto_functions);
call_graph();

if(functions_reached.find(callee)==functions_reached.end())
worklist.push_back(callee);
}
}
while(!worklist.empty());
std::unordered_set<irep_idt, irep_id_hash> functions_reached;
call_graph.compute_reachable(entry_point, functions_reached);

const irep_idt initialize=CPROVER_PREFIX "initialize";
functions_reached.erase(initialize);
Expand Down