-
Notifications
You must be signed in to change notification settings - Fork 273
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
$goto_instrument $args $name.gb | ||
|
||
$GC $NAME.c -o $NAME.gb | ||
$GI $OPTS $NAME.gb |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ | ||
-- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ | ||
-- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
|
@@ -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; | ||
|
@@ -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" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)" \ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.