CBMC
Loading...
Searching...
No Matches
goto2graph.h
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: Instrumenter
4
5Author: Vincent Nimal
6
7Date: 2012
8
9\*******************************************************************/
10
13
14#ifndef CPROVER_GOTO_INSTRUMENT_WMM_GOTO2GRAPH_H
15#define CPROVER_GOTO_INSTRUMENT_WMM_GOTO2GRAPH_H
16
17#include <map>
18
19#include <util/namespace.h>
20#include <util/message.h>
21
23
24#include "event_graph.h"
25#include "wmm.h"
26
27class value_setst;
28
30{
31public:
32 /* reference to goto-functions and symbol_table */
34
35protected:
37
38 /* alternative representation of graph (SCC) */
39 std::map<event_idt, event_idt> map_vertex_gnode;
41
42 unsigned unique_id;
43
44 /* rendering options */
48
49 bool inline local(const irep_idt &id);
50
51 void inline add_instr_to_interleaving(
52 goto_programt::instructionst::iterator it,
54
55 /* deprecated */
57
59
60 typedef std::set<event_grapht::critical_cyclet> set_of_cyclest;
61 void inline instrument_all_inserter(
62 const set_of_cyclest &set);
64 const set_of_cyclest &set);
66 const set_of_cyclest &set);
68 const set_of_cyclest &set);
70 const set_of_cyclest &set);
72 const set_of_cyclest &set, const std::set<event_idt> &events);
73
74 void inline print_outputs_local(
75 const std::set<event_grapht::critical_cyclet> &set,
76 std::ofstream &dot,
77 std::ofstream &ref,
78 std::ofstream &output,
79 std::ofstream &all,
80 std::ofstream &table,
81 memory_modelt model,
82 bool hide_internals);
83
84 typedef std::set<goto_programt::instructiont::targett> target_sett;
85
87 {
88 protected:
91
92 /* pointer to the egraph(s) that we construct */
94 std::vector<std::set<event_idt>> &egraph_SCCs;
96
97 /* for thread marking (dynamic) */
99 unsigned coming_from;
100
102 const irep_idt &function_id,
105 value_setst &value_sets
107 ,
109#endif
110 ) const; // NOLINT(whitespace/parens)
111
112 /* transformers */
113 void visit_cfg_thread() const;
114 void visit_cfg_propagate(goto_programt::instructionst::iterator i_it);
115 void visit_cfg_body(
116 const irep_idt &function_id,
117 const goto_programt &goto_program,
120 value_setst &value_sets
122 ,
124#endif
125 ); // deprecated NOLINT(whitespace/parens)
128 void inline visit_cfg_duplicate(
129 const goto_programt &goto_program,
132 void visit_cfg_assign(
133 value_setst &value_sets,
134 const irep_idt &function_id,
135 goto_programt::instructionst::iterator &i_it,
136 bool no_dependencies
138 ,
140#endif
141 ); // NOLINT(whitespace/parens)
142 void visit_cfg_fence(
143 goto_programt::instructionst::iterator i_it,
144 const irep_idt &function_id);
145 void visit_cfg_skip(goto_programt::instructionst::iterator i_it);
147 goto_programt::instructionst::iterator i_it,
148 const irep_idt &function_id);
150 goto_programt::instructionst::iterator i_it,
151 const irep_idt &function_id);
152 void visit_cfg_function_call(value_setst &value_sets,
153 goto_programt::instructionst::iterator i_it,
154 memory_modelt model,
155 bool no_dependenciess,
157 void visit_cfg_goto(
158 const irep_idt &function_id,
159 const goto_programt &goto_program,
160 goto_programt::instructionst::iterator i_it,
161 /* forces the duplication of all the loops, with array or not
162 otherwise, duplication of loops with array accesses only */
164 value_setst &value_sets
166 ,
168#endif
169 ); // NOLINT(whitespace/parens)
171
172 public:
174 {
175 }
176
177 unsigned max_thread;
178
179 /* relations between irep and Reads/Writes */
180 typedef std::multimap<irep_idt, event_idt> id2nodet;
181 typedef std::pair<irep_idt, event_idt> id2node_pairt;
183
185 unsigned read_counter;
186 unsigned ws_counter;
188
189 /* previous nodes (fwd analysis) */
190 typedef std::pair<event_idt, event_idt> nodet;
191 typedef std::map<
193 std::set<nodet>,
196
198 std::set<goto_programt::const_targett, goto_programt::target_less_than>
200
201 /* "next nodes" (bwd steps in fwd/bck analysis) */
203
204 #define add_all_pos(it, target, source) \
205 for(std::set<nodet>::const_iterator \
206 it=(source).begin(); \
207 it!=(source).end(); ++it) \
208 (target).insert(*it);
209
210 #ifdef CONTEXT_INSENSITIVE
211 /* to keep track of the functions (and their start/end nodes) */
212 std::stack<irep_idt> stack_fun;
214 std::map<irep_idt, std::set<nodet> > in_nodes, out_nodes;
215 #endif
216
217 /* current thread number */
218 unsigned thread;
219
220 /* dependencies */
222
223 /* writes and reads to unknown addresses -- conservative */
224 std::set<event_idt> unknown_read_nodes;
225 std::set<event_idt> unknown_write_nodes;
226
227 /* set of functions visited so far -- we don't handle recursive functions */
228 std::set<irep_idt> functions_met;
229
246
247 void inline enter_function(const irep_idt &function_id)
248 {
249 if(functions_met.find(function_id) != functions_met.end())
250 throw "sorry, doesn't handle recursive function for the moment";
251 functions_met.insert(function_id);
252 }
253
254 void inline leave_function(const irep_idt &function_id)
255 {
256 functions_met.erase(function_id);
257 }
258
259 void inline visit_cfg(
260 value_setst &value_sets,
261 memory_modelt model,
262 bool no_dependencies,
264 const irep_idt &function_id)
265 {
266 /* ignore recursive calls -- underapproximation */
267 try
268 {
269 /* forbids recursive function */
270 enter_function(function_id);
271 std::set<nodet> end_out;
273 value_sets,
274 model,
277 function_id,
278 end_out);
279 leave_function(function_id);
280 }
281 catch(const std::string &s)
282 {
284 }
285 }
286
295 virtual void visit_cfg_function(
296 value_setst &value_sets,
297 memory_modelt model,
298 bool no_dependencies,
300 const irep_idt &function_id,
301 std::set<nodet> &ending_vertex);
302
303 bool inline local(const irep_idt &i);
304 };
305
306public:
307 /* message */
309
310 /* graph */
312
313 /* graph split into strongly connected components */
314 std::vector<std::set<event_idt> > egraph_SCCs;
315
316 /* critical cycles */
317 std::set<event_grapht::critical_cyclet> set_of_cycles;
318
319 /* critical cycles per SCC */
320 std::vector<std::set<event_grapht::critical_cyclet> > set_of_cycles_per_SCC;
321 unsigned num_sccs;
322
323 /* map from function to begin and end of the corresponding part of the
324 graph */
325 typedef std::map<irep_idt, std::pair<std::set<event_idt>,
326 std::set<event_idt> > > map_function_nodest;
328
330 {
331 for(map_function_nodest::const_iterator it=map_function_graph.begin();
332 it!=map_function_graph.end();
333 ++it)
334 {
335 message.debug() << "FUNCTION " << it->first << ": " << messaget::eom;
336 message.debug() << "Start nodes: ";
337 for(std::set<event_idt>::const_iterator in_it=it->second.first.begin();
338 in_it!=it->second.first.end();
339 ++in_it)
340 message.debug() << *in_it << " ";
342 message.debug() << "End nodes: ";
343 for(std::set<event_idt>::const_iterator in_it=it->second.second.begin();
344 in_it!=it->second.second.end();
345 ++in_it)
346 message.debug() << *in_it << " ";
348 }
349 }
350
351 /* variables to instrument, locations of variables to instrument on
352 the cycles, and locations of all the variables on the critical cycles */
353 /* TODO: those maps are here to interface easily with weak_mem.cpp,
354 but a rewriting of weak_mem can eliminate them */
355 std::set<irep_idt> var_to_instr;
356 std::multimap<irep_idt, source_locationt> id2loc;
357 std::multimap<irep_idt, source_locationt> id2cycloc;
358
372
373 /* abstracts goto-programs in abstract event graph, and computes
374 the thread numbering and returns the max number */
375 unsigned goto2graph_cfg(
376 value_setst &value_sets,
377 memory_modelt model,
378 bool no_dependencies,
379 /* forces the duplication, with arrays or not; otherwise, arrays only */
381
382 /* collects directly all the cycles in the graph */
384 {
386 num_sccs = 0;
387 }
388
389 /* collects the cycles in the graph by SCCs */
391
392 /* filters cycles spurious by CFG */
393 void cfg_cycles_filter();
394
395 /* sets parameters for collection, if required */
397 unsigned _max_var = 0,
398 unsigned _max_po_trans = 0,
399 bool _ignore_arrays = false)
400 {
402 }
403
404 /* builds the relations between unsafe pairs in the critical cycles and
405 instructions to instrument in the code */
406
407 /* strategies for instrumentation */
409 void instrument_my_events(const std::set<event_idt> &events);
410
411 /* retrieves events to filter in the instrumentation choice
412 with option --my-events */
413 static std::set<event_idt> extract_my_events();
414
415 /* sets rendering options */
416 void set_rendering_options(bool aligned, bool file, bool function)
417 {
418 PRECONDITION(!file || !function);
419 render_po_aligned = aligned;
420 render_by_file = file;
421 render_by_function = function;
422 }
423
424 /* prints outputs:
425 - cycles.dot: graph of the instrumented cycles
426 - ref.txt: names of the instrumented cycles
427 - output.txt: names of the instructions in the code
428 - all.txt: all */
429 void print_outputs(memory_modelt model, bool hide_internals);
430};
431
432#endif // CPROVER_GOTO_INSTRUMENT_WMM_GOTO2GRAPH_H
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition dstring.h:38
void set_parameters_collection(unsigned _max_var=0, unsigned _max_po_trans=0, bool _ignore_arrays=false)
void collect_cycles(std::set< critical_cyclet > &set_of_cycles, memory_modelt model, const std::set< event_idt > &filter)
A collection of goto functions.
A generic container class for the GOTO intermediate representation of one function.
instructionst::const_iterator const_targett
void visit_cfg_backedge(goto_programt::const_targett targ, goto_programt::const_targett i_it)
strategy: fwd/bwd alternation
void visit_cfg_reference_function(irep_idt id_function)
references the first and last edges of the function
std::set< irep_idt > functions_met
Definition goto2graph.h:228
std::set< event_idt > unknown_read_nodes
Definition goto2graph.h:224
bool contains_shared_array(const irep_idt &function_id, goto_programt::const_targett targ, goto_programt::const_targett i_it, value_setst &value_sets) const
std::set< event_idt > unknown_write_nodes
Definition goto2graph.h:225
instrumentert & instrumenter
Definition goto2graph.h:90
void leave_function(const irep_idt &function_id)
Definition goto2graph.h:254
virtual void visit_cfg_function(value_setst &value_sets, memory_modelt model, bool no_dependencies, loop_strategyt duplicate_body, const irep_idt &function_id, std::set< nodet > &ending_vertex)
TODO: move the visitor outside, and inherit.
std::pair< irep_idt, event_idt > id2node_pairt
Definition goto2graph.h:181
bool local(const irep_idt &i)
void visit_cfg_fence(goto_programt::instructionst::iterator i_it, const irep_idt &function_id)
void visit_cfg_goto(const irep_idt &function_id, const goto_programt &goto_program, goto_programt::instructionst::iterator i_it, loop_strategyt replicate_body, value_setst &value_sets)
void visit_cfg_function_call(value_setst &value_sets, goto_programt::instructionst::iterator i_it, memory_modelt model, bool no_dependenciess, loop_strategyt duplicate_body)
std::multimap< irep_idt, event_idt > id2nodet
Definition goto2graph.h:180
std::map< goto_programt::const_targett, std::set< nodet >, goto_programt::target_less_than > incoming_post
Definition goto2graph.h:195
void visit_cfg_lwfence(goto_programt::instructionst::iterator i_it, const irep_idt &function_id)
std::vector< std::set< event_idt > > & egraph_SCCs
Definition goto2graph.h:94
void visit_cfg_propagate(goto_programt::instructionst::iterator i_it)
void visit_cfg_assign(value_setst &value_sets, const irep_idt &function_id, goto_programt::instructionst::iterator &i_it, bool no_dependencies)
void visit_cfg(value_setst &value_sets, memory_modelt model, bool no_dependencies, loop_strategyt duplicate_body, const irep_idt &function_id)
Definition goto2graph.h:259
void visit_cfg_duplicate(const goto_programt &goto_program, goto_programt::const_targett targ, goto_programt::const_targett i_it)
cfg_visitort(const namespacet &_ns, instrumentert &_instrumenter)
Definition goto2graph.h:230
void enter_function(const irep_idt &function_id)
Definition goto2graph.h:247
void visit_cfg_body(const irep_idt &function_id, const goto_programt &goto_program, goto_programt::const_targett i_it, loop_strategyt replicate_body, value_setst &value_sets)
strategy: fwd/bwd alternation
std::pair< event_idt, event_idt > nodet
Definition goto2graph.h:190
std::set< goto_programt::const_targett, goto_programt::target_less_than > updated
Definition goto2graph.h:199
void visit_cfg_skip(goto_programt::instructionst::iterator i_it)
void visit_cfg_asm_fence(goto_programt::instructionst::iterator i_it, const irep_idt &function_id)
std::multimap< irep_idt, source_locationt > id2cycloc
Definition goto2graph.h:357
void print_outputs(memory_modelt model, bool hide_internals)
void instrument_one_event_per_cycle_inserter(const set_of_cyclest &set)
instrumentert(goto_modelt &_goto_model, messaget &_message)
Definition goto2graph.h:359
unsigned num_sccs
Definition goto2graph.h:321
void instrument_my_events(const std::set< event_idt > &events)
void instrument_all_inserter(const set_of_cyclest &set)
void collect_cycles_by_SCCs(memory_modelt model)
Note: can be distributed (#define DISTRIBUTED)
event_grapht egraph
Definition goto2graph.h:311
void instrument_one_read_per_cycle_inserter(const set_of_cyclest &set)
std::set< event_grapht::critical_cyclet > set_of_cyclest
Definition goto2graph.h:60
std::set< event_grapht::critical_cyclet > set_of_cycles
Definition goto2graph.h:317
void collect_cycles(memory_modelt model)
Definition goto2graph.h:383
bool render_po_aligned
Definition goto2graph.h:45
void instrument_with_strategy(instrumentation_strategyt strategy)
std::set< irep_idt > var_to_instr
Definition goto2graph.h:355
std::map< irep_idt, std::pair< std::set< event_idt >, std::set< event_idt > > > map_function_nodest
Definition goto2graph.h:326
std::vector< std::set< event_idt > > egraph_SCCs
Definition goto2graph.h:314
void set_parameters_collection(unsigned _max_var=0, unsigned _max_po_trans=0, bool _ignore_arrays=false)
Definition goto2graph.h:396
void instrument_one_write_per_cycle_inserter(const set_of_cyclest &set)
std::multimap< irep_idt, source_locationt > id2loc
Definition goto2graph.h:356
bool is_cfg_spurious(const event_grapht::critical_cyclet &cyc)
bool render_by_function
Definition goto2graph.h:47
const namespacet ns
Definition goto2graph.h:33
map_function_nodest map_function_graph
Definition goto2graph.h:327
goto_functionst & goto_functions
Definition goto2graph.h:36
bool render_by_file
Definition goto2graph.h:46
std::map< event_idt, event_idt > map_vertex_gnode
Definition goto2graph.h:39
void instrument_my_events_inserter(const set_of_cyclest &set, const std::set< event_idt > &events)
std::set< goto_programt::instructiont::targett > target_sett
Definition goto2graph.h:84
unsigned unique_id
Definition goto2graph.h:42
void instrument_minimum_interference_inserter(const set_of_cyclest &set)
std::vector< std::set< event_grapht::critical_cyclet > > set_of_cycles_per_SCC
Definition goto2graph.h:320
wmm_grapht egraph_alt
Definition goto2graph.h:40
void add_instr_to_interleaving(goto_programt::instructionst::iterator it, goto_programt &interleaving)
messaget & message
Definition goto2graph.h:308
bool local(const irep_idt &id)
is local variable?
unsigned goto2graph_cfg(value_setst &value_sets, memory_modelt model, bool no_dependencies, loop_strategyt duplicate_body)
goes through CFG and build a static abstract event graph overapproximating the read/write relations f...
void cfg_cycles_filter()
unsigned cost(const event_grapht::critical_cyclet::delayt &e)
cost function
void set_rendering_options(bool aligned, bool file, bool function)
Definition goto2graph.h:416
static std::set< event_idt > extract_my_events()
void print_outputs_local(const std::set< event_grapht::critical_cyclet > &set, std::ofstream &dot, std::ofstream &ref, std::ofstream &output, std::ofstream &all, std::ofstream &table, memory_modelt model, bool hide_internals)
void print_map_function_graph() const
Definition goto2graph.h:329
Class that provides messages with a built-in verbosity 'level'.
Definition message.h:154
mstreamt & debug() const
Definition message.h:421
mstreamt & warning() const
Definition message.h:396
static eomt eom
Definition message.h:289
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
void dot(const goto_modelt &src, std::ostream &out)
Definition dot.cpp:359
graph of abstract events
Symbol Table + CFG.
#define PRECONDITION(CONDITION)
Definition invariant.h:463
A total order over targett and const_targett.
memory models
memory_modelt
Definition wmm.h:18
loop_strategyt
Definition wmm.h:37
instrumentation_strategyt
Definition wmm.h:27
@ all
Definition wmm.h:28