Skip to content

Remove initializations of unused global variables #331

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

Merged
merged 2 commits into from
Jan 10, 2017
Merged
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
16 changes: 9 additions & 7 deletions regression/goto-instrument/chain.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash

SRC=../../../src
src=../../../src
goto_cc=$src/goto-cc/goto-cc
goto_instrument=$src/goto-instrument/goto-instrument

GC=$SRC/goto-cc/goto-cc
GI=$SRC/goto-instrument/goto-instrument
name=${@:$#}
name=${name%.c}

OPTS=$1
NAME=${2%.c}
args=${@:1:$#-1}

$goto_cc -o $name.gb $name.c
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be nice if the changes to chain.sh and existing test.desc could be in a separate commit (even though still part of the this pull request) to explain what those changes are about.

$goto_instrument $args $name.gb

$GC $NAME.c -o $NAME.gb
$GI $OPTS $NAME.gb
2 changes: 1 addition & 1 deletion regression/goto-instrument/restore-returns1/test.desc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CORE
ret.c
"--escape-analysis --dump-c"
--escape-analysis --dump-c
^EXIT=0$
^SIGNAL=0$
--
Expand Down
2 changes: 1 addition & 1 deletion regression/goto-instrument/restore-returns2/test.desc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CORE
ret.c
"--escape-analysis --dump-c"
--escape-analysis --dump-c
^EXIT=0$
^SIGNAL=0$
--
Expand Down
42 changes: 42 additions & 0 deletions regression/goto-instrument/slice-global-inits1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

int x;
int y;

int z;

int a[10];

typedef struct some_struct {
int a;
int b;
} some_struct_t;

some_struct_t s1;
some_struct_t s2;

void func1()
{
s1.a = 7;
}

void func2()
{
s2.a = 7;
}

void func3()
{
func3();
}

int main()
{
z = 1;
z = a[0];

func2();

func3();

return 0;
}
13 changes: 13 additions & 0 deletions regression/goto-instrument/slice-global-inits1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CORE
main.c
--slice-global-inits --show-goto-functions
z = 0;$
a =
s2 =
^EXIT=0$
^SIGNAL=0$
--
x = 0;$
y = 0;$
s1 =
^warning: ignoring
9 changes: 9 additions & 0 deletions src/goto-instrument/goto_instrument_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Author: Daniel Kroening, [email protected]
#include <goto-programs/remove_asm.h>
#include <goto-programs/remove_unused_functions.h>
#include <goto-programs/parameter_assignments.h>
#include <goto-programs/slice_global_inits.h>

#include <pointer-analysis/value_set_analysis.h>
#include <pointer-analysis/goto_program_dereference.h>
Expand Down Expand Up @@ -1015,6 +1016,13 @@ void goto_instrument_parse_optionst::instrument_goto_program()
nondet_static(ns, goto_functions);
}

if(cmdline.isset("slice-global-inits"))
{
status() << "Slicing away initializations of unused global variables"
<< eom;
slice_global_inits(ns, goto_functions);
}

if(cmdline.isset("string-abstraction"))
{
status() << "String Abstraction" << eom;
Expand Down Expand Up @@ -1371,6 +1379,7 @@ void goto_instrument_parse_optionst::help()
" --reachability-slice slice away instructions that can't reach assertions\n"
" --full-slice slice away instructions that don't affect assertions\n"
" --property id slice with respect to specific property only\n"
" --slice-global-inits slice away initializations of unused global variables\n"
"\n"
"Further transformations:\n"
" --constant-propagator propagate constants and simplify expressions\n"
Expand Down
2 changes: 1 addition & 1 deletion src/goto-instrument/goto_instrument_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Author: Daniel Kroening, [email protected]
"(custom-bitvector-analysis)" \
"(show-struct-alignment)(interval-analysis)(show-intervals)" \
"(show-uninitialized)(show-locations)" \
"(full-slice)(reachability-slice)" \
"(full-slice)(reachability-slice)(slice-global-inits)" \
"(inline)(remove-function-pointers)" \
"(show-claims)(show-properties)(property):" \
"(show-symbol-table)(show-points-to)(show-rw-set)" \
Expand Down
3 changes: 2 additions & 1 deletion src/goto-programs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ SRC = goto_convert.cpp goto_convert_function_call.cpp \
remove_returns.cpp osx_fat_reader.cpp remove_complex.cpp \
goto_trace.cpp xml_goto_trace.cpp vcd_goto_trace.cpp \
graphml_witness.cpp remove_virtual_functions.cpp \
class_hierarchy.cpp show_goto_functions.cpp get_goto_model.cpp
class_hierarchy.cpp show_goto_functions.cpp get_goto_model.cpp \
slice_global_inits.cpp

INCLUDES= -I ..

Expand Down
140 changes: 140 additions & 0 deletions src/goto-programs/slice_global_inits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*******************************************************************\

Module: Remove initializations of unused global variables

Author: Daniel Poetzl

Date: December 2016

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

#include <analyses/call_graph.h>

#include <util/namespace.h>
#include <util/std_expr.h>
#include <util/cprover_prefix.h>
#include <util/prefix.h>
#include <util/hash_cont.h>

#include <goto-programs/goto_functions.h>
#include <goto-programs/remove_skip.h>

#include "slice_global_inits.h"

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

Function: slice_global_inits

Inputs:

Outputs:

Purpose:

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

void slice_global_inits(
const namespacet &ns,
goto_functionst &goto_functions)
{
// 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;
hash_set_cont<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);

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;

if(functions_reached.find(callee)==functions_reached.end())
worklist.push_back(callee);
}
} while(!worklist.empty());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Note to self: there is a patch to add functionality just listing all reachable functions, which has very similar code. I should file a pull request for that one.


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

// gather all symbols used by reachable functions

class symbol_collectort:public const_expr_visitort
{
public:
virtual void operator()(const exprt &expr)
{
if(expr.id()==ID_symbol)
{
const symbol_exprt &symbol_expr=to_symbol_expr(expr);
const irep_idt id=symbol_expr.get_identifier();
symbols.insert(id);
}
}

hash_set_cont<irep_idt, irep_id_hash> symbols;
};

symbol_collectort visitor;

assert(!functions_reached.empty());

for(const irep_idt &id : functions_reached)
{
const goto_functionst::goto_functiont &goto_function
=goto_functions.function_map.at(id);
const goto_programt &goto_program=goto_function.body;

forall_goto_program_instructions(i_it, goto_program)
{
const codet &code=i_it->code;
code.visit(visitor);
}
}

const hash_set_cont<irep_idt, irep_id_hash> &symbols=visitor.symbols;

// now remove unnecessary initializations

goto_functionst::function_mapt::iterator f_it;
f_it=goto_functions.function_map.find(initialize);
assert(f_it!=goto_functions.function_map.end());

goto_programt &goto_program=f_it->second.body;

Forall_goto_program_instructions(i_it, goto_program)
{
if(i_it->is_assign())
{
const code_assignt &code_assign=to_code_assign(i_it->code);
const symbol_exprt &symbol_expr=to_symbol_expr(code_assign.lhs());
const irep_idt id=symbol_expr.get_identifier();

if(!has_prefix(id2string(id), CPROVER_PREFIX) &&
symbols.find(id)==symbols.end())
i_it->make_skip();
}
}

remove_skip(goto_functions);
goto_functions.update();
}
21 changes: 21 additions & 0 deletions src/goto-programs/slice_global_inits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************\

Module: Remove initializations of unused global variables

Author: Daniel Poetzl

Date: December 2016

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

#ifndef CPROVER_GOTO_PROGRAMS_SLICE_GLOBAL_INITS_H
#define CPROVER_GOTO_PROGRAMS_SLICE_GLOBAL_INITS_H

class goto_functionst;
class namespacet;

void slice_global_inits(
const namespacet &ns,
goto_functionst &goto_functions);

#endif