CBMC
reaching_definitions.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Range-based reaching definitions analysis (following Field-
4  Sensitive Program Dependence Analysis, Litvak et al., FSE 2010)
5 
6 Author: Michael Tautschnig
7 
8 Date: February 2013
9 
10 \*******************************************************************/
11 
24 
25 #ifndef CPROVER_ANALYSES_REACHING_DEFINITIONS_H
26 #define CPROVER_ANALYSES_REACHING_DEFINITIONS_H
27 
28 #include <util/threeval.h>
29 
30 #include "ai.h"
31 #include "goto_rw.h"
32 
33 class value_setst;
34 class is_threadedt;
35 class dirtyt;
37 
41 template<typename V>
43 {
44 public:
45  const V &get(const std::size_t value_index) const
46  {
47  assert(value_index<values.size());
48  return values[value_index]->first;
49  }
50 
51  std::size_t add(const V &value)
52  {
53  inner_mapt &m=value_map[value.identifier];
54 
55  std::pair<typename inner_mapt::iterator, bool> entry=
56  m.insert(std::make_pair(value, values.size()));
57 
58  if(entry.second)
59  values.push_back(entry.first);
60 
61  return entry.first->second;
62  }
63 
64  void clear()
65  {
66  value_map.clear();
67  values.clear();
68  }
69 
70 protected:
71  typedef typename std::map<V, std::size_t> inner_mapt;
76  std::vector<typename inner_mapt::const_iterator> values;
80  std::unordered_map<irep_idt, inner_mapt> value_map;
81 };
82 
86 {
97 
99  const irep_idt &identifier,
101  const range_spect &bit_begin,
102  const range_spect &bit_end)
107  {
108  }
109 };
110 
113 inline bool operator<(
114  const reaching_definitiont &a,
115  const reaching_definitiont &b)
116 {
118  return true;
120  return false;
121 
123  return a.bit_begin.is_unknown();
124 
125  if(!a.bit_begin.is_unknown())
126  {
127  if(a.bit_begin < b.bit_begin)
128  return true;
129  if(b.bit_begin < a.bit_begin)
130  return false;
131  }
132 
133  if(a.bit_end.is_unknown() != b.bit_end.is_unknown())
134  return a.bit_end.is_unknown();
135 
136  if(!a.bit_end.is_unknown())
137  {
138  if(a.bit_end < b.bit_end)
139  return true;
140  if(b.bit_end < a.bit_end)
141  return false;
142  }
143 
144  // we do not expect comparison of unrelated definitions
145  // as this operator< is only used in sparse_bitvector_analysist
146  assert(a.identifier==b.identifier);
147 
148  return false;
149 }
150 
158 {
159 public:
162  : ai_domain_baset(), has_values(false), bv_container(_bv_container)
163  {
164  PRECONDITION(bv_container != nullptr);
165  }
166 
179  void transform(
180  const irep_idt &function_from,
181  trace_ptrt trace_from,
182  const irep_idt &function_to,
183  trace_ptrt trace_to,
184  ai_baset &ai,
185  const namespacet &ns) final override;
186 
187  void output(
188  std::ostream &out,
189  const ai_baset &,
190  const namespacet &) const final override
191  {
192  output(out);
193  }
194 
195  void make_top() final override
196  {
197  values.clear();
198  if(bv_container)
199  bv_container->clear();
200  has_values=tvt(true);
201  }
202 
203  void make_bottom() final override
204  {
205  values.clear();
206  if(bv_container)
207  bv_container->clear();
208  has_values=tvt(false);
209  }
210 
211  void make_entry() final override
212  {
213  make_top();
214  }
215 
216  bool is_top() const override final
217  {
218  DATA_INVARIANT(!has_values.is_true() || values.empty(),
219  "If domain is top, the value map must be empty");
220  return has_values.is_true();
221  }
222 
223  bool is_bottom() const override final
224  {
226  "If domain is bottom, the value map must be empty");
227  return has_values.is_false();
228  }
229 
242  bool merge(const rd_range_domaint &other, trace_ptrt from, trace_ptrt to);
243 
244  bool merge_shared(
245  const rd_range_domaint &other,
246  locationt from,
247  locationt to,
248  const namespacet &ns);
249 
250  // each element x represents a range of bits [x.first, x.second)
251  typedef std::multimap<range_spect, range_spect> rangest;
252  typedef std::map<locationt, rangest> ranges_at_loct;
253 
254  const ranges_at_loct &get(const irep_idt &identifier) const;
255  void clear_cache(const irep_idt &identifier) const
256  {
257  export_cache[identifier].clear();
258  }
259 
260 private:
265 
274 
275  typedef std::set<std::size_t> values_innert;
276  #ifdef USE_DSTRING
277  typedef std::map<irep_idt, values_innert> valuest;
278  #else
279  typedef std::unordered_map<irep_idt, values_innert> valuest;
280  #endif
281  valuest values;
287 
288  #ifdef USE_DSTRING
289  typedef std::map<irep_idt, ranges_at_loct> export_cachet;
290  #else
291  typedef std::unordered_map<irep_idt, ranges_at_loct> export_cachet;
292  #endif
293  mutable export_cachet export_cache;
305 
306  void populate_cache(const irep_idt &identifier) const;
307 
308  void transform_dead(
309  const namespacet &ns,
310  locationt from);
312  const namespacet &ns,
315  const namespacet &ns,
316  const irep_idt &function_from,
317  locationt from,
318  const irep_idt &function_to,
321  const namespacet &ns,
322  const irep_idt &function_from,
323  locationt from,
324  const irep_idt &function_to,
325  locationt to,
327  void transform_assign(
328  const namespacet &ns,
329  locationt from,
330  const irep_idt &function_to,
331  locationt to,
333 
334  void kill(
335  const irep_idt &identifier,
336  const range_spect &range_start,
337  const range_spect &range_end);
338  void kill_inf(
339  const irep_idt &identifier,
340  const range_spect &range_start);
341  bool gen(
342  locationt from,
343  const irep_idt &identifier,
344  const range_spect &range_start,
345  const range_spect &range_end);
346 
347  void output(std::ostream &out) const;
348 
349  bool merge_inner(
350  values_innert &dest,
351  const values_innert &other);
352 };
353 
355  public concurrency_aware_ait<rd_range_domaint>,
356  public sparse_bitvector_analysist<reaching_definitiont>
357 {
358 public:
359  // constructor
360  explicit reaching_definitions_analysist(const namespacet &_ns);
361 
363 
364  void initialize(const goto_functionst &goto_functions) override;
365 
367  {
368  assert(value_sets);
369  return *value_sets;
370  }
371 
373  {
374  assert(is_threaded);
375  return *is_threaded;
376  }
377 
378  const dirtyt &get_is_dirty() const
379  {
380  assert(is_dirty);
381  return *is_dirty;
382  }
383 
384 protected:
385  const namespacet &ns;
386  std::unique_ptr<value_setst> value_sets;
387  std::unique_ptr<is_threadedt> is_threaded;
388  std::unique_ptr<dirtyt> is_dirty;
389 };
390 
391 #endif // CPROVER_ANALYSES_REACHING_DEFINITIONS_H
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
rd_range_domaint::is_top
bool is_top() const override final
Definition: reaching_definitions.h:216
rd_range_domaint::values
valuest values
It is an ordered map from program variable names to IDs of reaching_definitiont instances stored in m...
Definition: reaching_definitions.h:286
rd_range_domaint::populate_cache
void populate_cache(const irep_idt &identifier) const
Given the passed variable name identifier it collects data from bv_container for each ID in values[id...
Definition: reaching_definitions.cpp:71
rd_range_domaint::merge
bool merge(const rd_range_domaint &other, trace_ptrt from, trace_ptrt to)
Implements the "join" operation of two instances *this and other.
Definition: reaching_definitions.cpp:634
sparse_bitvector_analysist< reaching_definitiont >::inner_mapt
std::map< reaching_definitiont, std::size_t > inner_mapt
Definition: reaching_definitions.h:71
range_spect::is_unknown
bool is_unknown() const
Definition: goto_rw.h:74
reaching_definitions_analysist::reaching_definitions_analysist
reaching_definitions_analysist(const namespacet &_ns)
Definition: reaching_definitions.cpp:53
dirtyt
Dirty variables are ones which have their address taken so we can't reliably work out where they may ...
Definition: dirty.h:27
reaching_definitions_analysist::get_value_sets
value_setst & get_value_sets() const
Definition: reaching_definitions.h:366
threeval.h
sparse_bitvector_analysist
An instance of this class provides an assignment of unique numeric ID to each inserted reaching_defin...
Definition: reaching_definitions.h:42
reaching_definitiont::definition_at
ai_domain_baset::locationt definition_at
The iterator to the GOTO instruction where the variable has been written to.
Definition: reaching_definitions.h:91
rd_range_domaint::transform_assign
void transform_assign(const namespacet &ns, locationt from, const irep_idt &function_to, locationt to, reaching_definitions_analysist &rd)
Definition: reaching_definitions.cpp:303
range_spect
Data type to describe upper and lower bounds of the range of bits that a read or write access may aff...
Definition: goto_rw.h:60
sparse_bitvector_analysist::value_map
std::unordered_map< irep_idt, inner_mapt > value_map
A map from names of program variables to a set of pairs (reaching_definitiont, ID).
Definition: reaching_definitions.h:80
rd_range_domaint::ranges_at_loct
std::map< locationt, rangest > ranges_at_loct
Definition: reaching_definitions.h:252
reaching_definitions_analysist::get_is_threaded
const is_threadedt & get_is_threaded() const
Definition: reaching_definitions.h:372
sparse_bitvector_analysist::values
std::vector< typename inner_mapt::const_iterator > values
It is a map from an ID to the corresponding reaching_definitiont instance inside the map value_map.
Definition: reaching_definitions.h:76
rd_range_domaint::values_innert
std::set< std::size_t > values_innert
Definition: reaching_definitions.h:275
rd_range_domaint::has_values
tvt has_values
This (three value logic) flag determines, whether the instance represents top, bottom,...
Definition: reaching_definitions.h:264
rd_range_domaint::bv_container
sparse_bitvector_analysist< reaching_definitiont > *const bv_container
It points to the actual reaching definitions data of individual program variables.
Definition: reaching_definitions.h:273
rd_range_domaint::make_bottom
void make_bottom() final override
no states
Definition: reaching_definitions.h:203
reaching_definitiont::bit_begin
range_spect bit_begin
The two integers below define a range of bits (i.e.
Definition: reaching_definitions.h:95
rd_range_domaint::get
const ranges_at_loct & get(const irep_idt &identifier) const
Definition: reaching_definitions.cpp:719
ai_domain_baset::trace_ptrt
ai_history_baset::trace_ptrt trace_ptrt
Definition: ai_domain.h:74
reaching_definitions_analysist
Definition: reaching_definitions.h:354
goto_rw.h
is_threadedt
Definition: is_threaded.h:21
sparse_bitvector_analysist::add
std::size_t add(const V &value)
Definition: reaching_definitions.h:51
rd_range_domaint::transform_function_call
void transform_function_call(const namespacet &ns, const irep_idt &function_from, locationt from, const irep_idt &function_to, reaching_definitions_analysist &rd)
Definition: reaching_definitions.cpp:176
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
reaching_definitions_analysist::is_threaded
std::unique_ptr< is_threadedt > is_threaded
Definition: reaching_definitions.h:387
DATA_INVARIANT
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition: invariant.h:510
reaching_definitiont::identifier
irep_idt identifier
The name of the variable which was defined.
Definition: reaching_definitions.h:88
rd_range_domaint::is_bottom
bool is_bottom() const override final
Definition: reaching_definitions.h:223
PRECONDITION
#define PRECONDITION(CONDITION)
Definition: invariant.h:463
sparse_bitvector_analysist::clear
void clear()
Definition: reaching_definitions.h:64
rd_range_domaint
Because the class is inherited from ai_domain_baset, its instance represents an element of a domain o...
Definition: reaching_definitions.h:157
rd_range_domaint::transform
void transform(const irep_idt &function_from, trace_ptrt trace_from, const irep_idt &function_to, trace_ptrt trace_to, ai_baset &ai, const namespacet &ns) final override
Computes an instance obtained from the instance *this by transformation over a GOTO instruction refer...
Definition: reaching_definitions.cpp:91
rd_range_domaint::gen
bool gen(locationt from, const irep_idt &identifier, const range_spect &range_start, const range_spect &range_end)
A utility function which updates internal data structures by inserting a new reaching definition reco...
Definition: reaching_definitions.cpp:479
operator<
bool operator<(const reaching_definitiont &a, const reaching_definitiont &b)
In order to use instances of this structure as keys in ordered containers, such as std::map,...
Definition: reaching_definitions.h:113
rd_range_domaint::export_cachet
std::map< irep_idt, ranges_at_loct > export_cachet
Definition: reaching_definitions.h:289
ai.h
tvt::is_false
bool is_false() const
Definition: threeval.h:26
rd_range_domaint::rangest
std::multimap< range_spect, range_spect > rangest
Definition: reaching_definitions.h:251
tvt
Definition: threeval.h:19
rd_range_domaint::merge_inner
bool merge_inner(values_innert &dest, const values_innert &other)
Definition: reaching_definitions.cpp:584
rd_range_domaint::transform_dead
void transform_dead(const namespacet &ns, locationt from)
Computes an instance obtained from a *this by transformation over DEAD v GOTO instruction.
Definition: reaching_definitions.cpp:136
reaching_definitions_analysist::~reaching_definitions_analysist
virtual ~reaching_definitions_analysist()
goto_functionst
A collection of goto functions.
Definition: goto_functions.h:24
ai_domain_baset::locationt
goto_programt::const_targett locationt
Definition: ai_domain.h:73
value_setst
Definition: value_sets.h:21
rd_range_domaint::make_entry
void make_entry() final override
Make this domain a reasonable entry-point state.
Definition: reaching_definitions.h:211
rd_range_domaint::transform_start_thread
void transform_start_thread(const namespacet &ns, reaching_definitions_analysist &rd)
Definition: reaching_definitions.cpp:151
reaching_definitions_analysist::get_is_dirty
const dirtyt & get_is_dirty() const
Definition: reaching_definitions.h:378
rd_range_domaint::make_top
void make_top() final override
all states – the analysis doesn't use this, and domains may refuse to implement it.
Definition: reaching_definitions.h:195
rd_range_domaint::merge_shared
bool merge_shared(const rd_range_domaint &other, locationt from, locationt to, const namespacet &ns)
Definition: reaching_definitions.cpp:670
reaching_definitiont::reaching_definitiont
reaching_definitiont(const irep_idt &identifier, const ai_domain_baset::locationt &definition_at, const range_spect &bit_begin, const range_spect &bit_end)
Definition: reaching_definitions.h:98
concurrency_aware_ait
Base class for concurrency-aware abstract interpretation.
Definition: ai.h:652
ai_baset
This is the basic interface of the abstract interpreter with default implementations of the core func...
Definition: ai.h:118
reaching_definitions_analysist::initialize
void initialize(const goto_functionst &goto_functions) override
Initialize all the abstract states for a whole program.
Definition: reaching_definitions.cpp:734
rd_range_domaint::rd_range_domaint
rd_range_domaint(sparse_bitvector_analysist< reaching_definitiont > *_bv_container)
Definition: reaching_definitions.h:160
reaching_definitiont::bit_end
range_spect bit_end
Definition: reaching_definitions.h:96
rd_range_domaint::export_cache
export_cachet export_cache
It is a helper data structure.
Definition: reaching_definitions.h:304
rd_range_domaint::kill_inf
void kill_inf(const irep_idt &identifier, const range_spect &range_start)
Definition: reaching_definitions.cpp:441
reaching_definitiont
Identifies a GOTO instruction where a given variable is defined (i.e.
Definition: reaching_definitions.h:85
reaching_definitions_analysist::ns
const namespacet & ns
Definition: reaching_definitions.h:385
rd_range_domaint::kill
void kill(const irep_idt &identifier, const range_spect &range_start, const range_spect &range_end)
Definition: reaching_definitions.cpp:341
rd_range_domaint::output
void output(std::ostream &out, const ai_baset &, const namespacet &) const final override
Definition: reaching_definitions.h:187
rd_range_domaint::transform_end_function
void transform_end_function(const namespacet &ns, const irep_idt &function_from, locationt from, const irep_idt &function_to, locationt to, reaching_definitions_analysist &rd)
Definition: reaching_definitions.cpp:241
ai_domain_baset
The interface offered by a domain, allows code to manipulate domains without knowing their exact type...
Definition: ai_domain.h:54
reaching_definitions_analysist::is_dirty
std::unique_ptr< dirtyt > is_dirty
Definition: reaching_definitions.h:388
rd_range_domaint::clear_cache
void clear_cache(const irep_idt &identifier) const
Definition: reaching_definitions.h:255
rd_range_domaint::valuest
std::map< irep_idt, values_innert > valuest
Definition: reaching_definitions.h:277
sparse_bitvector_analysist::get
const V & get(const std::size_t value_index) const
Definition: reaching_definitions.h:45
reaching_definitions_analysist::value_sets
std::unique_ptr< value_setst > value_sets
Definition: reaching_definitions.h:386
tvt::is_true
bool is_true() const
Definition: threeval.h:25