cprover
value_set_fivr.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Value Set (Flow Insensitive, Sharing, Validity Regions)
4 
5 Author: Daniel Kroening, kroening@kroening.com
6  CM Wintersteiger
7 
8 \*******************************************************************/
9 
12 
13 #ifndef CPROVER_POINTER_ANALYSIS_VALUE_SET_FIVR_H
14 #define CPROVER_POINTER_ANALYSIS_VALUE_SET_FIVR_H
15 
16 #include <list>
17 #include <map>
18 #include <unordered_set>
19 
20 #include <util/mp_arith.h>
21 #include <util/namespace.h>
23 #include <util/invariant.h>
24 
25 #include "object_numbering.h"
26 
28 {
29 public:
31  {
32  }
33 
38 
39  void set_from(const irep_idt &function, unsigned inx)
40  {
41  from_function = function_numbering.number(function);
42  from_target_index = inx;
43  }
44 
45  void set_to(const irep_idt &function, unsigned inx)
46  {
47  to_function = function_numbering.number(function);
48  to_target_index = inx;
49  }
50 
51  typedef irep_idt idt;
52 
56  bool offset_is_zero(const offsett &offset) const
57  {
58  return offset && offset->is_zero();
59  }
60 
62  {
63  public:
65  static const object_map_dt blank;
66 
67  typedef std::map<object_numberingt::number_type, offsett> objmapt;
69 
70  // NOLINTNEXTLINE(readability/identifiers)
71  typedef objmapt::const_iterator const_iterator;
72  // NOLINTNEXTLINE(readability/identifiers)
73  typedef objmapt::iterator iterator;
74 
76  {
77  return objmap.find(k);
78  }
79  iterator begin() { return objmap.begin(); }
80  const_iterator begin() const { return objmap.begin(); }
81  iterator end() { return objmap.end(); }
82  const_iterator end() const { return objmap.end(); }
83  size_t size() const { return objmap.size(); }
84  bool empty() const { return objmap.empty(); }
85  void clear() { objmap.clear(); validity_ranges.clear(); }
86 
88  {
89  return objmap[k];
90  }
91 
92  // operator[] is the only way to insert something!
93  std::pair<iterator, bool>
94  insert(const std::pair<object_numberingt::number_type, offsett> &)
95  {
97  }
98  iterator
99  insert(iterator, const std::pair<object_numberingt::number_type, offsett> &)
100  {
101  UNREACHABLE;
102  }
103 
105  {
106  public:
107  unsigned function;
108  unsigned from, to;
109 
111  function(0), from(0), to(0)
112  {
113  }
114 
115  validity_ranget(unsigned fnc, unsigned f, unsigned t):
116  function(fnc), from(f), to(t)
117  {
118  }
119 
120  bool contains(unsigned f, unsigned line) const
121  {
122  return (function==f && from<=line && line<=to);
123  }
124  };
125 
126  typedef std::list<validity_ranget> vrange_listt;
127  typedef std::map<unsigned, vrange_listt> validity_rangest;
129 
130  bool set_valid_at(unsigned inx, unsigned f, unsigned line);
131  bool set_valid_at(unsigned inx, const validity_ranget &vr);
132  bool is_valid_at(unsigned inx, unsigned f, unsigned line) const;
133  };
134 
136 
138 
139  void set(object_mapt &dest, object_map_dt::const_iterator it) const
140  {
141  dest.write()[it->first]=it->second;
142  }
143 
145  {
146  return insert_to(dest, it->first, it->second);
147  }
148 
149  bool insert_to(object_mapt &dest, const exprt &src) const
150  {
151  return insert_to(dest, object_numbering.number(src), offsett());
152  }
153 
154  bool insert_to(
155  object_mapt &dest,
156  const exprt &src,
157  const mp_integer &offset_value) const
158  {
159  return insert_to(dest, object_numbering.number(src), offsett(offset_value));
160  }
161 
162  bool insert_to(
163  object_mapt &dest,
165  const offsett &offset) const;
166 
167  bool
168  insert_to(object_mapt &dest, const exprt &expr, const offsett &offset) const
169  {
170  return insert_to(dest, object_numbering.number(expr), offset);
171  }
172 
174  {
175  return insert_from(dest, it->first, it->second);
176  }
177 
178  bool insert_from(object_mapt &dest, const exprt &src) const
179  {
180  return insert_from(dest, object_numbering.number(src), offsett());
181  }
182 
184  object_mapt &dest,
185  const exprt &src,
186  const mp_integer &offset_value) const
187  {
188  return insert_from(
189  dest, object_numbering.number(src), offsett(offset_value));
190  }
191 
192  bool insert_from(
193  object_mapt &dest,
195  const offsett &offset) const;
196 
197  bool
198  insert_from(object_mapt &dest, const exprt &expr, const offsett &offset) const
199  {
200  return insert_from(dest, object_numbering.number(expr), offset);
201  }
202 
203  struct entryt
204  {
207  std::string suffix;
208 
209  entryt() { }
210 
211  entryt(const idt &_identifier, const std::string _suffix):
212  identifier(_identifier),
213  suffix(_suffix)
214  {
215  }
216  };
217 
218  typedef std::unordered_set<exprt, irep_hash> expr_sett;
219 
220  typedef std::unordered_set<unsigned int> dynamic_object_id_sett;
221 
222  #ifdef USE_DSTRING
223  typedef std::map<idt, entryt> valuest;
224  typedef std::unordered_set<idt> flatten_seent;
225  typedef std::unordered_set<idt> gvs_recursion_sett;
226  typedef std::unordered_set<idt> recfind_recursion_sett;
227  typedef std::unordered_set<idt> assign_recursion_sett;
228  #else
229  typedef std::unordered_map<idt, entryt, string_hash> valuest;
230  typedef std::unordered_set<idt, string_hash> flatten_seent;
231  typedef std::unordered_set<idt, string_hash> gvs_recursion_sett;
232  typedef std::unordered_set<idt, string_hash> recfind_recursion_sett;
233  typedef std::unordered_set<idt, string_hash> assign_recursion_sett;
234  #endif
235 
236  void get_value_set(
237  const exprt &expr,
238  std::list<exprt> &expr_set,
239  const namespacet &ns) const;
240 
241  expr_sett &get(
242  const idt &identifier,
243  const std::string &suffix);
244 
245  void make_any()
246  {
247  values.clear();
248  }
249 
250  void clear()
251  {
252  values.clear();
253  }
254 
255  void add_var(const idt &id, const std::string &suffix)
256  {
257  get_entry(id, suffix);
258  }
259 
260  void add_var(const entryt &e)
261  {
263  }
264 
265  entryt &get_entry(const idt &id, const std::string &suffix)
266  {
267  return get_entry(entryt(id, suffix));
268  }
269 
271  {
272  std::string index=id2string(e.identifier)+e.suffix;
273 
274  std::pair<valuest::iterator, bool> r=
275  values.insert(std::pair<idt, entryt>(index, e));
276 
277  return r.first->second;
278  }
279 
280  entryt &get_temporary_entry(const idt &id, const std::string &suffix)
281  {
282  std::string index=id2string(id)+suffix;
283  return temporary_values[index];
284  }
285 
286  void add_vars(const std::list<entryt> &vars)
287  {
288  for(std::list<entryt>::const_iterator
289  it=vars.begin();
290  it!=vars.end();
291  it++)
292  add_var(*it);
293  }
294 
295  void output(
296  const namespacet &ns,
297  std::ostream &out) const;
298 
301 
302  // true = added something new
303  bool make_union(
304  object_mapt &dest,
305  const object_mapt &src) const;
306 
307  bool make_valid_union(
308  object_mapt &dest,
309  const object_mapt &src) const;
310 
311  void copy_objects(
312  object_mapt &dest,
313  const object_mapt &src) const;
314 
315  void apply_code(
316  const exprt &code,
317  const namespacet &ns);
318 
319  bool handover();
320 
321  void assign(
322  const exprt &lhs,
323  const exprt &rhs,
324  const namespacet &ns,
325  bool add_to_sets=false);
326 
327  void do_function_call(
328  const irep_idt &function,
329  const exprt::operandst &arguments,
330  const namespacet &ns);
331 
332  // edge back to call site
333  void do_end_function(
334  const exprt &lhs,
335  const namespacet &ns);
336 
337  void get_reference_set(
338  const exprt &expr,
339  expr_sett &expr_set,
340  const namespacet &ns) const;
341 
342 protected:
344  const exprt &expr,
345  expr_sett &expr_set,
346  const namespacet &ns) const;
347 
348  void get_value_set_rec(
349  const exprt &expr,
350  object_mapt &dest,
351  const std::string &suffix,
352  const typet &original_type,
353  const namespacet &ns,
354  gvs_recursion_sett &recursion_set) const;
355 
356  void get_value_set(
357  const exprt &expr,
358  object_mapt &dest,
359  const namespacet &ns) const;
360 
362  const exprt &expr,
363  object_mapt &dest,
364  const namespacet &ns) const
365  {
366  get_reference_set_sharing_rec(expr, dest, ns);
367  }
368 
370  const exprt &expr,
371  object_mapt &dest,
372  const namespacet &ns) const;
373 
374  void dereference_rec(
375  const exprt &src,
376  exprt &dest) const;
377 
378  void assign_rec(
379  const exprt &lhs,
380  const object_mapt &values_rhs,
381  const std::string &suffix,
382  const namespacet &ns,
383  assign_recursion_sett &recursion_set,
384  bool add_to_sets);
385 
386  void do_free(
387  const exprt &op,
388  const namespacet &ns);
389 
390  void flatten(
391  const entryt &e,
392  object_mapt &dest) const;
393 
394  void flatten_rec(
395  const entryt&,
396  object_mapt&,
397  flatten_seent&,
398  unsigned from_function) const;
399 
400  bool recursive_find(
401  const irep_idt &ident,
402  const object_mapt &rhs,
403  recfind_recursion_sett &recursion_set) const;
404 };
405 
406 #endif // CPROVER_POINTER_ANALYSIS_VALUE_SET_FIVR_H
The type of an expression.
Definition: type.h:22
void get_reference_set_sharing(const exprt &expr, object_mapt &dest, const namespacet &ns) const
void get_reference_set(const exprt &expr, expr_sett &expr_set, const namespacet &ns) const
static int8_t r
Definition: irep_hash.h:59
BigInt mp_integer
Definition: mp_arith.h:22
number_type number(const key_type &a)
Definition: numbering.h:37
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
void dereference_rec(const exprt &src, exprt &dest) const
void do_end_function(const exprt &lhs, const namespacet &ns)
const_iterator end() const
void add_var(const entryt &e)
unsigned from_function
void set_from(const irep_idt &function, unsigned inx)
bool recursive_find(const irep_idt &ident, const object_mapt &rhs, recfind_recursion_sett &recursion_set) const
unsigned to_target_index
bool make_union(object_mapt &dest, const object_mapt &src) const
void copy_objects(object_mapt &dest, const object_mapt &src) const
void output(const namespacet &ns, std::ostream &out) const
std::unordered_set< exprt, irep_hash > expr_sett
unsigned to_function
unsigned from_target_index
bool offset_is_zero(const offsett &offset) const
void get_reference_set_sharing_rec(const exprt &expr, object_mapt &dest, const namespacet &ns) const
void set_to(const irep_idt &function, unsigned inx)
validity_ranget(unsigned fnc, unsigned f, unsigned t)
iterator insert(iterator, const std::pair< object_numberingt::number_type, offsett > &)
void get_value_set_rec(const exprt &expr, object_mapt &dest, const std::string &suffix, const typet &original_type, const namespacet &ns, gvs_recursion_sett &recursion_set) const
bool insert_from(object_mapt &dest, const exprt &expr, const offsett &offset) const
bool insert_from(object_mapt &dest, object_map_dt::const_iterator it) const
bool is_valid_at(unsigned inx, unsigned f, unsigned line) const
bool insert_from(object_mapt &dest, const exprt &src) const
nonstd::optional< T > optionalt
Definition: optional.h:35
void apply_code(const exprt &code, const namespacet &ns)
entryt & get_entry(const entryt &e)
TO_BE_DOCUMENTED.
Definition: namespace.h:74
std::list< validity_ranget > vrange_listt
void do_function_call(const irep_idt &function, const exprt::operandst &arguments, const namespacet &ns)
valuest temporary_values
std::unordered_map< idt, entryt, string_hash > valuest
void get_reference_set_sharing(const exprt &expr, expr_sett &expr_set, const namespacet &ns) const
std::unordered_set< idt, string_hash > gvs_recursion_sett
void assign(const exprt &lhs, const exprt &rhs, const namespacet &ns, bool add_to_sets=false)
void add_vars(const std::list< entryt > &vars)
void get_value_set(const exprt &expr, std::list< exprt > &expr_set, const namespacet &ns) const
bool make_valid_union(object_mapt &dest, const object_mapt &src) const
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
entryt & get_entry(const idt &id, const std::string &suffix)
static object_numberingt object_numbering
typename Map::mapped_type number_type
Definition: numbering.h:24
void flatten(const entryt &e, object_mapt &dest) const
static const object_map_dt blank
bool insert_from(object_mapt &dest, const exprt &src, const mp_integer &offset_value) const
bool insert_to(object_mapt &dest, const exprt &src, const mp_integer &offset_value) const
std::unordered_set< unsigned int > dynamic_object_id_sett
void flatten_rec(const entryt &, object_mapt &, flatten_seent &, unsigned from_function) const
entryt & get_temporary_entry(const idt &id, const std::string &suffix)
std::vector< exprt > operandst
Definition: expr.h:45
bool set_valid_at(unsigned inx, unsigned f, unsigned line)
exprt to_expr(object_map_dt::const_iterator it) const
optionalt< mp_integer > offsett
Represents the offset into an object: either a unique integer offset, or an unknown value...
objmapt::const_iterator const_iterator
void assign_rec(const exprt &lhs, const object_mapt &values_rhs, const std::string &suffix, const namespacet &ns, assign_recursion_sett &recursion_set, bool add_to_sets)
bool insert_to(object_mapt &dest, object_map_dt::const_iterator it) const
reference_counting< object_map_dt > object_mapt
bool insert_to(object_mapt &dest, const exprt &expr, const offsett &offset) const
Base class for all expressions.
Definition: expr.h:42
Reference Counting.
static hash_numbering< irep_idt, irep_id_hash > function_numbering
std::unordered_set< idt, string_hash > flatten_seent
#define UNREACHABLE
Definition: invariant.h:271
std::map< unsigned, vrange_listt > validity_rangest
entryt(const idt &_identifier, const std::string _suffix)
const_iterator begin() const
std::pair< iterator, bool > insert(const std::pair< object_numberingt::number_type, offsett > &)
void add_var(const idt &id, const std::string &suffix)
std::unordered_set< idt, string_hash > assign_recursion_sett
bool contains(unsigned f, unsigned line) const
void do_free(const exprt &op, const namespacet &ns)
offsett & operator[](object_numberingt::number_type k)
std::unordered_set< idt, string_hash > recfind_recursion_sett
bool insert_to(object_mapt &dest, const exprt &src) const
const_iterator find(object_numberingt::number_type k)
std::map< object_numberingt::number_type, offsett > objmapt