CBMC
constant_propagator.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Constant propagation
4 
5 Author: Peter Schrammel
6 
7 \*******************************************************************/
8 
21 
22 #ifndef CPROVER_ANALYSES_CONSTANT_PROPAGATOR_H
23 #define CPROVER_ANALYSES_CONSTANT_PROPAGATOR_H
24 
25 #include <iosfwd>
26 #include <util/replace_symbol.h>
27 
28 #include "ai.h"
29 #include "dirty.h"
30 
32 
34 {
35 public:
36  virtual void transform(
37  const irep_idt &function_from,
38  trace_ptrt trace_from,
39  const irep_idt &function_to,
40  trace_ptrt trace_to,
41  ai_baset &ai_base,
42  const namespacet &ns) final override;
43 
44  virtual void output(
45  std::ostream &out,
46  const ai_baset &ai_base,
47  const namespacet &ns) const override;
48 
49  bool merge(
50  const constant_propagator_domaint &other,
51  trace_ptrt from,
52  trace_ptrt to);
53 
54  virtual bool ai_simplify(
55  exprt &condition,
56  const namespacet &ns) const final override;
57 
58  virtual void make_bottom() final override
59  {
61  }
62 
63  virtual void make_top() final override
64  {
66  }
67 
68  virtual bool is_bottom() const final override
69  {
70  return values.is_bot();
71  }
72 
73  virtual bool is_top() const final override
74  {
75  return values.is_top();
76  }
77 
78  struct valuest
79  {
80  // maps variables to constants
82  bool is_bottom = true;
83 
84  bool merge(const valuest &src);
85  bool meet(const valuest &src, const namespacet &ns);
86 
87  // set whole state
88 
90  {
92  is_bottom=true;
93  }
94 
95  void set_to_top()
96  {
98  is_bottom=false;
99  }
100 
101  bool is_bot() const
102  {
103  return is_bottom && replace_const.empty();
104  }
105 
106  bool is_top() const
107  {
108  return !is_bottom && replace_const.empty();
109  }
110 
111  void set_to(const symbol_exprt &lhs, const exprt &rhs)
112  {
113  replace_const.set(lhs, rhs);
114  is_bottom=false;
115  }
116 
117  bool set_to_top(const symbol_exprt &expr);
118 
119  void set_dirty_to_top(const dirtyt &dirty, const namespacet &ns);
120 
121  bool is_constant(const exprt &expr, const namespacet &ns) const;
122 
123  bool is_constant(const irep_idt &id, const namespacet &ns) const;
124 
125  bool is_empty() const
126  {
127  return replace_const.empty();
128  }
129 
130  void output(std::ostream &out, const namespacet &ns) const;
131  };
132 
134 
135  static bool partial_evaluate(
136  const valuest &known_values,
137  exprt &expr,
138  const namespacet &ns);
139 
140 protected:
141  static void assign_rec(
142  valuest &dest_values,
143  const exprt &lhs,
144  const exprt &rhs,
145  const namespacet &ns,
146  const constant_propagator_ait *cp,
147  bool is_assignment);
148 
150  const exprt &expr,
151  const namespacet &ns,
152  const constant_propagator_ait *cp);
153 
155  const valuest &known_values,
156  exprt &expr,
157  const namespacet &ns);
158 
159  static bool replace_constants_and_simplify(
160  const valuest &known_values,
161  exprt &expr,
162  const namespacet &ns);
163 };
164 
165 class constant_propagator_ait:public ait<constant_propagator_domaint>
166 {
167 public:
168  typedef std::function<bool(const exprt &, const namespacet &)>
170 
171  static bool track_all_values(const exprt &, const namespacet &)
172  {
173  return true;
174  }
175 
177  const goto_functionst &goto_functions,
179  dirty(goto_functions),
181  {
182  }
183 
185  const goto_functiont &goto_function,
187  dirty(goto_function),
189  {
190  }
191 
193  goto_modelt &goto_model,
195  dirty(goto_model.goto_functions),
197  {
198  const namespacet ns(goto_model.symbol_table);
199  operator()(goto_model.goto_functions, ns);
200  replace(goto_model.goto_functions, ns);
201  }
202 
204  const irep_idt &function_identifier,
205  goto_functionst::goto_functiont &goto_function,
206  const namespacet &ns,
208  : dirty(goto_function), should_track_value(should_track_value)
209  {
210  operator()(function_identifier, goto_function, ns);
211  replace(goto_function, ns);
212  }
213 
215 
216 protected:
218 
219  void replace(
221  const namespacet &);
222 
223  void replace(
224  goto_functionst &,
225  const namespacet &);
226 
227  void replace_types_rec(
228  const replace_symbolt &replace_const,
229  exprt &expr);
230 
232 };
233 
234 #endif // CPROVER_ANALYSES_CONSTANT_PROPAGATOR_H
Abstract Interpretation.
Replace symbols with constants while maintaining syntactically valid expressions.
This is the basic interface of the abstract interpreter with default implementations of the core func...
Definition: ai.h:117
void operator()(const irep_idt &function_id, const goto_programt &goto_program, const namespacet &ns)
Run abstract interpretation on a single function.
Definition: ai.h:143
The interface offered by a domain, allows code to manipulate domains without knowing their exact type...
Definition: ai_domain.h:54
ai_history_baset::trace_ptrt trace_ptrt
Definition: ai_domain.h:73
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition: ai.h:562
std::function< bool(const exprt &, const namespacet &)> should_track_valuet
constant_propagator_ait(goto_modelt &goto_model, should_track_valuet should_track_value=track_all_values)
void replace_types_rec(const replace_symbolt &replace_const, exprt &expr)
static bool track_all_values(const exprt &, const namespacet &)
constant_propagator_ait(const goto_functionst &goto_functions, should_track_valuet should_track_value=track_all_values)
should_track_valuet should_track_value
constant_propagator_ait(const irep_idt &function_identifier, goto_functionst::goto_functiont &goto_function, const namespacet &ns, should_track_valuet should_track_value=track_all_values)
constant_propagator_ait(const goto_functiont &goto_function, should_track_valuet should_track_value=track_all_values)
void replace(goto_functionst::goto_functiont &, const namespacet &)
static void assign_rec(valuest &dest_values, const exprt &lhs, const exprt &rhs, const namespacet &ns, const constant_propagator_ait *cp, bool is_assignment)
Assign value rhs to lhs, recording any newly-known constants in dest_values.
virtual bool is_bottom() const final override
virtual bool is_top() const final override
static bool partial_evaluate(const valuest &known_values, exprt &expr, const namespacet &ns)
Attempt to evaluate expression using domain knowledge This function changes the expression that is pa...
virtual bool ai_simplify(exprt &condition, const namespacet &ns) const final override
Simplify the condition given context-sensitive knowledge from the abstract state.
static bool partial_evaluate_with_all_rounding_modes(const valuest &known_values, exprt &expr, const namespacet &ns)
Attempt to evaluate an expression in all rounding modes.
virtual void transform(const irep_idt &function_from, trace_ptrt trace_from, const irep_idt &function_to, trace_ptrt trace_to, ai_baset &ai_base, const namespacet &ns) final override
how function calls are treated: a) there is an edge from each call site to the function head b) there...
static bool replace_constants_and_simplify(const valuest &known_values, exprt &expr, const namespacet &ns)
bool two_way_propagate_rec(const exprt &expr, const namespacet &ns, const constant_propagator_ait *cp)
handles equalities and conjunctions containing equalities
virtual void output(std::ostream &out, const ai_baset &ai_base, const namespacet &ns) const override
virtual void make_top() final override
all states – the analysis doesn't use this directly (see make_entry) and domains may refuse to implem...
bool merge(const constant_propagator_domaint &other, trace_ptrt from, trace_ptrt to)
virtual void make_bottom() final override
no states
Dirty variables are ones which have their address taken so we can't reliably work out where they may ...
Definition: dirty.h:28
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
A collection of goto functions.
::goto_functiont goto_functiont
A goto function, consisting of function body (see body) and parameter identifiers (see parameter_iden...
Definition: goto_function.h:24
symbol_tablet symbol_table
Symbol table.
Definition: goto_model.h:31
goto_functionst goto_functions
GOTO functions.
Definition: goto_model.h:34
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:94
Replace a symbol expression by a given expression.
bool empty() const
void set(const class symbol_exprt &old_expr, const exprt &new_expr)
Sets old_expr to be replaced by new_expr.
Expression to hold a symbol (variable)
Definition: std_expr.h:131
Variables whose address is taken.
bool is_constant(const exprt &expr, const namespacet &ns) const
bool meet(const valuest &src, const namespacet &ns)
meet
void output(std::ostream &out, const namespacet &ns) const
void set_dirty_to_top(const dirtyt &dirty, const namespacet &ns)
address_of_aware_replace_symbolt replace_const
void set_to(const symbol_exprt &lhs, const exprt &rhs)