Skip to content

Commit fe285ec

Browse files
committed
Slice global inits: update the symbol table
Update the symbol table and auto-generate __CPROVER_initialize instead of hand-tweaking the function. This will ensure that any further rebuilding of __CPROVER_initialize preserves those changes.
1 parent 5ed8232 commit fe285ec

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ void goto_instrument_parse_optionst::instrument_goto_program()
13761376
{
13771377
log.status() << "Slicing away initializations of unused global variables"
13781378
<< messaget::eom;
1379-
slice_global_inits(goto_model);
1379+
slice_global_inits(goto_model, ui_message_handler);
13801380
}
13811381

13821382
if(cmdline.isset("string-abstraction"))
@@ -1663,7 +1663,7 @@ void goto_instrument_parse_optionst::instrument_goto_program()
16631663

16641664
log.status() << "Slicing away initializations of unused global variables"
16651665
<< messaget::eom;
1666-
slice_global_inits(goto_model);
1666+
slice_global_inits(goto_model, ui_message_handler);
16671667

16681668
log.status() << "Performing an aggressive slice" << messaget::eom;
16691669
aggressive_slicert aggressive_slicer(goto_model, ui_message_handler);

src/goto-programs/slice_global_inits.cpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ Date: December 2016
2020
#include <util/cprover_prefix.h>
2121
#include <util/prefix.h>
2222

23-
#include <util/invariant.h>
24-
25-
#include <goto-programs/goto_functions.h>
26-
#include <goto-programs/goto_model.h>
27-
#include <goto-programs/remove_skip.h>
28-
2923
#include <linking/static_lifetime_init.h>
3024

31-
void slice_global_inits(goto_modelt &goto_model)
25+
#include "goto_convert_functions.h"
26+
#include "goto_functions.h"
27+
#include "goto_model.h"
28+
29+
void slice_global_inits(
30+
goto_modelt &goto_model,
31+
message_handlert &message_handler)
3232
{
3333
// gather all functions reachable from the entry point
3434
const irep_idt entry_point=goto_functionst::entry_point();
@@ -115,22 +115,35 @@ void slice_global_inits(goto_modelt &goto_model)
115115
}
116116

117117
// now remove unnecessary initializations
118-
for(auto &instruction : goto_program.instructions)
118+
bool changed = false;
119+
for(auto &entry : goto_model.symbol_table)
119120
{
120-
if(instruction.is_assign())
121+
if(
122+
entry.second.is_static_lifetime && !entry.second.is_type &&
123+
!entry.second.is_macro && entry.second.type.id() != ID_code &&
124+
!has_prefix(id2string(entry.first), CPROVER_PREFIX) &&
125+
symbols_to_keep.find(entry.first) == symbols_to_keep.end())
121126
{
122-
const symbol_exprt &symbol_expr =
123-
to_symbol_expr(instruction.assign_lhs());
124-
const irep_idt id=symbol_expr.get_identifier();
125-
126-
if(
127-
!has_prefix(id2string(id), CPROVER_PREFIX) &&
128-
symbols_to_keep.find(id) == symbols_to_keep.end())
129-
{
130-
instruction.turn_into_skip();
131-
}
127+
symbolt &symbol = goto_model.symbol_table.get_writeable_ref(entry.first);
128+
symbol.is_extern = true;
129+
symbol.value.make_nil();
130+
symbol.value.set(ID_C_no_nondet_initialization, 1);
131+
changed = true;
132132
}
133133
}
134134

135-
remove_skip(goto_functions);
135+
if(
136+
changed &&
137+
goto_model.goto_functions.function_map.erase(INITIALIZE_FUNCTION) != 0)
138+
{
139+
static_lifetime_init(
140+
goto_model.symbol_table,
141+
goto_model.symbol_table.lookup_ref(INITIALIZE_FUNCTION).location);
142+
goto_convert(
143+
INITIALIZE_FUNCTION,
144+
goto_model.symbol_table,
145+
goto_model.goto_functions,
146+
message_handler);
147+
goto_model.goto_functions.update();
148+
}
136149
}

src/goto-programs/slice_global_inits.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Date: December 2016
1717
#include <util/exception_utils.h>
1818

1919
class goto_modelt;
20+
class message_handlert;
2021

2122
class user_input_error_exceptiont : public cprover_exception_baset
2223
{
@@ -35,6 +36,11 @@ class user_input_error_exceptiont : public cprover_exception_baset
3536
std::string message;
3637
};
3738

38-
void slice_global_inits(goto_modelt &);
39+
/// Remove initialization of global variables that are not used in any function
40+
/// reachable from the entry point of \p goto_model.
41+
/// Warnings are reported via \p message_handler.
42+
void slice_global_inits(
43+
goto_modelt &goto_model,
44+
message_handlert &message_handler);
3945

4046
#endif

src/linking/static_lifetime_init.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ static_lifetime_init(const irep_idt &identifier, symbol_tablet &symbol_table)
7272
if((symbol.value.is_nil() && symbol.is_extern) ||
7373
symbol.value.id() == ID_nondet)
7474
{
75+
if(symbol.value.get_bool(ID_C_no_nondet_initialization))
76+
return {};
77+
7578
// Nondet initialise if not linked, or if explicitly requested.
7679
// Compilers would usually complain about the unlinked symbol case.
7780
const auto nondet = nondet_initializer(symbol.type, symbol.location, ns);

0 commit comments

Comments
 (0)