|
| 1 | +/*******************************************************************\ |
| 2 | +
|
| 3 | +Module: Remove initializations of unused global variables |
| 4 | +
|
| 5 | +Author: Daniel Poetzl |
| 6 | +
|
| 7 | +Date: December 2016 |
| 8 | +
|
| 9 | +\*******************************************************************/ |
| 10 | + |
| 11 | +#include <analyses/call_graph.h> |
| 12 | + |
| 13 | +#include <util/namespace.h> |
| 14 | +#include <util/std_expr.h> |
| 15 | +#include <util/cprover_prefix.h> |
| 16 | +#include <util/prefix.h> |
| 17 | +#include <util/hash_cont.h> |
| 18 | + |
| 19 | +#include <goto-programs/goto_functions.h> |
| 20 | +#include <goto-programs/remove_skip.h> |
| 21 | + |
| 22 | +#include "slice_global_inits.h" |
| 23 | + |
| 24 | +/*******************************************************************\ |
| 25 | +
|
| 26 | +Function: slice_global_inits |
| 27 | +
|
| 28 | + Inputs: |
| 29 | +
|
| 30 | + Outputs: |
| 31 | +
|
| 32 | + Purpose: |
| 33 | +
|
| 34 | +\*******************************************************************/ |
| 35 | + |
| 36 | +void slice_global_inits( |
| 37 | + const namespacet &ns, |
| 38 | + goto_functionst &goto_functions) |
| 39 | +{ |
| 40 | + // gather all functions reachable from the entry point |
| 41 | + |
| 42 | + call_grapht call_graph(goto_functions); |
| 43 | + const call_grapht::grapht &graph=call_graph.graph; |
| 44 | + |
| 45 | + std::list<irep_idt> worklist; |
| 46 | + hash_set_cont<irep_idt, irep_id_hash> functions_reached; |
| 47 | + |
| 48 | + const irep_idt entry_point=goto_functionst::entry_point(); |
| 49 | + |
| 50 | + goto_functionst::function_mapt::const_iterator e_it; |
| 51 | + e_it=goto_functions.function_map.find(entry_point); |
| 52 | + |
| 53 | + if(e_it==goto_functions.function_map.end()) |
| 54 | + throw "entry point not found"; |
| 55 | + |
| 56 | + worklist.push_back(entry_point); |
| 57 | + |
| 58 | + do |
| 59 | + { |
| 60 | + const irep_idt id=worklist.front(); |
| 61 | + worklist.pop_front(); |
| 62 | + |
| 63 | + functions_reached.insert(id); |
| 64 | + |
| 65 | + const auto &p=graph.equal_range(id); |
| 66 | + |
| 67 | + for(auto it=p.first; it!=p.second; it++) |
| 68 | + { |
| 69 | + const irep_idt callee=it->second; |
| 70 | + |
| 71 | + if(functions_reached.find(callee)==functions_reached.end()) |
| 72 | + worklist.push_back(callee); |
| 73 | + } |
| 74 | + } while(!worklist.empty()); |
| 75 | + |
| 76 | + const irep_idt initialize=CPROVER_PREFIX "initialize"; |
| 77 | + functions_reached.erase(initialize); |
| 78 | + |
| 79 | + // gather all symbols used by reachable functions |
| 80 | + |
| 81 | + class symbol_collectort:public const_expr_visitort |
| 82 | + { |
| 83 | + public: |
| 84 | + virtual void operator()(const exprt &expr) |
| 85 | + { |
| 86 | + if(expr.id()==ID_symbol) |
| 87 | + { |
| 88 | + const symbol_exprt &symbol_expr=to_symbol_expr(expr); |
| 89 | + const irep_idt id=symbol_expr.get_identifier(); |
| 90 | + symbols.insert(id); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + hash_set_cont<irep_idt, irep_id_hash> symbols; |
| 95 | + }; |
| 96 | + |
| 97 | + symbol_collectort visitor; |
| 98 | + |
| 99 | + assert(!functions_reached.empty()); |
| 100 | + |
| 101 | + for(const irep_idt &id : functions_reached) |
| 102 | + { |
| 103 | + const goto_functionst::goto_functiont &goto_function |
| 104 | + =goto_functions.function_map.at(id); |
| 105 | + const goto_programt &goto_program=goto_function.body; |
| 106 | + |
| 107 | + forall_goto_program_instructions(i_it, goto_program) |
| 108 | + { |
| 109 | + const codet &code=i_it->code; |
| 110 | + code.visit(visitor); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + const hash_set_cont<irep_idt, irep_id_hash> &symbols=visitor.symbols; |
| 115 | + |
| 116 | + // now remove unnecessary initializations |
| 117 | + |
| 118 | + goto_functionst::function_mapt::iterator f_it; |
| 119 | + f_it=goto_functions.function_map.find(initialize); |
| 120 | + assert(f_it!=goto_functions.function_map.end()); |
| 121 | + |
| 122 | + goto_programt &goto_program=f_it->second.body; |
| 123 | + |
| 124 | + Forall_goto_program_instructions(i_it, goto_program) |
| 125 | + { |
| 126 | + if(i_it->is_assign()) |
| 127 | + { |
| 128 | + const code_assignt &code_assign=to_code_assign(i_it->code); |
| 129 | + const symbol_exprt &symbol_expr=to_symbol_expr(code_assign.lhs()); |
| 130 | + const irep_idt id=symbol_expr.get_identifier(); |
| 131 | + |
| 132 | + if(!has_prefix(id2string(id), CPROVER_PREFIX) && |
| 133 | + symbols.find(id)==symbols.end()) |
| 134 | + i_it->make_skip(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + remove_skip(goto_functions); |
| 139 | + goto_functions.update(); |
| 140 | +} |
0 commit comments